0

Possible Duplicate:
Hibernate: different object with the same identifier value was already associated with the session

I am new to Hibernate and couldn't understand this from api. The api for session.get says:

Object org.hibernate.Session.get(Class clazz, Serializable id)

Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance. (If the instance is already associated with the session, return that instance. This method never returns an uninitialized instance.)

What is meant identifier here. In most of the examples they give it as 1L (probably because primary key in the table was Long?) I do not understand its essence.

Community
  • 1
  • 1
Jatin
  • 31,116
  • 15
  • 98
  • 163

1 Answers1

3

What is meant identifier here

The identifier for an entity is the property annotated with @Id or @EmbeddedId.

If you class uses a long surrogate identifier you would use session as follows:

session.get(MyClass.class, 1);

If your class uses an embedded id you would pass an instance of that @Embeddable class to the get method.

session.get(MyClass.class, new MyClassIdentifier("String", 1));
Alex Barnes
  • 7,174
  • 1
  • 30
  • 50