3

I have created a simple class and a simple tables of database to test the hibernate concepts. The class is named "Employee":

public class Employee {
   private String firstName; 
   private String lastName;   
   private int salary;  
   public Employee(String fname, String lname, int salary) {
      this.firstName = fname;
      this.lastName = lname;
      this.salary = salary;
   }
   ...
}

The database names "EMPLOYEE":

create table EMPLOYEE (
   id INT NOT NULL auto_increment,
   first_name VARCHAR(20) default NULL,
   last_name  VARCHAR(20) default NULL,
   salary     INT  default NULL,
   PRIMARY KEY (id)
);

This is my Employee.hbm.xml:

<hibernate-mapping>
   <class name="Employee" table="EMPLOYEE">
      <id name="id" type="int" column="id">
         <generator class="native"/>
      </id>
      ...
      ...
   </class>
</hibernate-mapping>

So, you can notice that in my class it doesn't have the "id" attribute. However, I create the id column in my database to let it be the primary key which is automatically generated. In this case, I don't know what should I put in the <id name="?" type="int" column="id"> to map my class with my database. If I ignore it, would it be some problems afterward?

2
  • won't this will depend on how your db handles 'auto_increment' and why would you not want to map this in the class? Commented Jun 14, 2013 at 10:20
  • @domfarr: So, I don't have to define it in the Employee.hbm.xml, do I? However, I think it would be some problems afterward. Commented Jun 14, 2013 at 10:27

3 Answers 3

3

I can understand where you are coming from, but Hibernate identifies Objects (in first and 2nd level cache) using the ID of the object, so an object without an ID cannot be represented in hibernate.

I would recommend you to create the field with generator class as native and let the db deal with the value.

so in annotation you'd say

@Id @GeneratedValue(strategy=GenerationType.NATIVE)
private Integer id;

if you don't want to repeat this entry multiple times you can create a base class (base pojo) which has this and extend the class in all your models.

Sign up to request clarification or add additional context in comments.

Comments

2

You can use Composite Key Concept , if you don't want to use @Id, You can use @EmbededId annotation.

Comments

1

In my experience, you should keep the id as an attribute of the POJO (Employee class). The reason for this is because the id may be needed when you want to search for a particular Employee object in the database (Hibernate Session's get() and load() methods require the id). In addition, because you have mapped the id as an attribute in the Hibernate Mapping file, it should therefore correspond with the POJO (Employee class) definition of properties/attributes.

2 Comments

I am agree with you but there are some situations that a class doesn't belong to me. So, I cannot modify it. I have to create a database follow a user requirement.
@Maverick then you have to wrap the class and provide your own id or use a translator object to translate between the class you don't own and your persisted representation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.