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
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.
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.
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.
@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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With