用Hibernate使用提供的ID保存新对象 [英] Save new object with Hibernate using supplied ID
问题描述
我想用Hibernate将一些对象保存到具有预定义ID的数据库中。使用Hibernate会话的保存方法可以做到吗?
我知道有以下解决方法:
1)使用必要的插入语句执行SQL脚本:
insert into MyObj(id,name)values(100,'aaa'),(101,'bbb');
2)在Hibernate中使用SQL查询:
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();
返回true;
} else {
return false;
$ / code>
但是:是否可以不使用SQLQuery?
在 11.2。使对象持久化有以下代码示例:
或者,您可以使用重载版本分配
标识符
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));
但我无法找到如何执行此操作的示例。
那么,是否可以使用Hibernate将使用提供的ID的新对象保存到数据库而不使用SQL查询?如果是,那么如何?
如何在Hibernate参考中提到的如何重载会话方法save()?
谢谢!
文档要求您不要覆盖 save
,但要使用带有两个参数的 save
变体。
YourEntity entity = // ..
entity.setFoo (富);
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)
I want to save some Objects to database with predefined IDs using Hibernate. Is it possible to do it using save method of Hibernate session?
I know there are following workarounds:
1) execute SQL script with necessary insert statements:
insert into MyObj(id,name) values (100,'aaa'), (101,'bbb');
2) use 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 Hibernate reference in section 11.2. Making objects persistent there is example of code:
Alternatively, you can assign the identifier using an 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 couldn't find example of how to do this.
So, is it possible to save new objects with supplied IDs to database with Hibernate not using SQL query? If yes then how? And how to overload session method save() as mentioned in Hibernate reference?
Thanks!
The documentation asks you not to override save
yourself, but to use the variant of save
which takes two arguments. See the [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)
这篇关于用Hibernate使用提供的ID保存新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!