Save a new object using Hibernate using the supplied identifier

I want to store some objects in a database with predefined identifiers using Hibernate. Can this be done using the Hibernate session persistence method?

I know that the following workarounds exist:

1) execute the SQL script with the necessary insert statements:

insert into MyObj(id,name) values (100,'aaa'), (101,'bbb'); 

2) use the SQL query in Hibernate:

 public static boolean createObj(Long id, String name) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); if (session.get(MyObj.class, id) == null) { session.beginTransaction(); SQLQuery sqlQuery = session.createSQLQuery ("insert into myobj(id,name) values(?,?)"); sqlQuery.setLong(0, id); sqlQuery.setString(1, name); sqlQuery.executeUpdate(); session.getTransaction().commit(); return true; } else { return false; } } 

But: is it possible to do without SQLQuery?

In the Hibernate link in section 11.2. Constantly saving objects , there is an example code:

Alternatively, you can assign an identifier using the overloaded version of save ().

 DomesticCat pk = new DomesticCat(); pk.setColor(Color.TABBY); pk.setSex('F'); pk.setName("PK"); pk.setKittens( new HashSet() ); pk.addKitten(fritz); sess.save( pk, new Long(1234) ); 

But I could not find an example of how to do this.

So, is it possible to save new objects with provided identifiers to the database using Hibernate without using an SQL query? If so, how? And how to overload the session save () method as specified in Hibernate?

Thanks!

+7
java hibernate session
source share
5 answers

The documentation suggests that you do not override save yourself, but use the save option, which takes two arguments. See [JavaDocs] [1] for more details.

 YourEntity entity = // .. entity.setFoo(foo); entity.setBar(bar); //.. session.save(entity, theIDYouWantToUse); 

[1]: http://docs.jboss.org/hibernate/core/3.6/javadocs/org/hibernate/Session.html#save(java.lang.String , java.lang.Object)

+4
source share

Use session.replicate

 YourEntity entity = // .. entity.setId(yourId); entity.setBar(bar); //.. session.replicate(entity,ReplicationMode.EXCEPTION); 
+4
source share

Specify assigned as a generator in the ID column.

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/mapping.html#mapping-declaration-id-generator

Note that in order for this to be good, it helps to have a property with version zero. Hibernate then treats the null version values โ€‹โ€‹as unsaved.

 <id name="id" type="int"> <generator class="assigned" /> </id> <version name="version" type="int"> public class Person { // ID; assigned, SPEC. public int getId(); public void setId (int id); // Version; NOTE -- must be nullable! public Integer getVersion(); public void setVersion (Integer version); } 

Hope this helps.

+1
source share

At the moment, replicate(Object object, ReplicationMode replicationMode) seems acceptable and preferable. You can play a little with different values โ€‹โ€‹for ReplicationMode according to your needs.

0
source share

Use the save method in sleep mode: - save (String entityName, Object) For more information, kindly follow: - http://docs.jboss.org/hibernate/core/3.6/javadocs/org/hibernate/Session.html# save (java.lang.String , java.lang.Object)

Note. - Do not use GeneratedValue of any type on top of your main @GeneratedValue key (strategy = GenerationType.IDENTITY)

0
source share

All Articles