Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what am i missing for using hibernate annotation?

i am trying to create a basic hibernate entity POJO using latest hibernate and i have added the necesary jar files i downloaded from hibernate website.

the problem is when i add the line @Table(name = "user")

it complains of a compile error:

The annotation @Table must define the attribute appliesTo

full code below:

package com.jr.entities.users;

import java.io.Serializable;

import org.hibernate.annotations.Entity;
import org.hibernate.annotations.Table;

@Entity
@Table(name = "user")
public class DAOuser implements Serializable{
    
    private String uid;
    private String emailAddress;
    private String username;
    private String password;
    

}

In this example link http://www.roseindia.net/hibernate/hibernateannotations/hibernate-annotations-tutorial.shtml it says that it does not need to applyTo value to be set? Am I missing something? I created a simple EJB3 project in eclipse J2ee if it helps.

Thanks in advance

like image 628
jonney Avatar asked Apr 19 '11 16:04

jonney


People also ask

Why do we use annotations in hibernate?

Hibernate Annotations is the powerful way to provide the metadata for the Object and Relational Table mapping. All the metadata is clubbed into the POJO java file along with the code, this helps the user to understand the table structure and POJO simultaneously during the development.

Why we are using JPA annotation instead of hibernate?

You use hibernate as implementation of JPA API. You should be able to change hibernate with another implementation (like EclipseLink) without changing in the code. This is why you should only use JPA annotations.

Which of the following two are the mandatory annotations on a JPA entity?

The JPA specification requires the @Entity annotation. It identifies a class as an entity class. You can use the name attribute of the @Entity annotation to define the name of the entity. It has to be unique for the persistence unit, and you use it to reference the entity in your JPQL queries.

What is the use of @entity annotation in hibernate?

@Entity annotation marks this class as an entity. @Table annotation specifies the table name where data of this entity is to be persisted. If you don't use @Table annotation, hibernate will use the class name as the table name by default.


1 Answers

There are two sets of persistence annotations (@Entity and @Table) - JPA annotations (in package javax.persistence) and Hibernate annotations (in package org.hibernate.annotations). Note that example uses JPA annotations, whereas your code uses Hibernate annotations, so your code doesn't compile because these annotations have different sets of attributes.

So, you need to change packages in your import statements.

Usually you should use JPA annotations unless you need some features provided only by Hibernate annotations.

like image 123
axtavt Avatar answered Sep 28 '22 04:09

axtavt