在 Hibernate 中防止 select 查询时插入
我是休眠新手。我有一个问题,当我尝试运行选择查询
"from Foo where Foo.some_id=2"
(使用 Hibernate 模板)时,Hibernate 也会尝试将记录插入到与 Foo 表
Bean Foo
class Foo{
int id;
....
Foo2 foo2;
}
Foo 具有一对二一关联的表“Foo2”中.hbm.xml
...
<one-to-one name="foo2" class="Foo2" property-ref="foo"
constrained="false" cascade="save-update"></one-to-one>
...
Bean Foo2
Class Foo2{
...
private int foo;
...
}
Foo2.hbm.xml
...
<property name="foo" column="foo_id"/>
...
用法
DetachedCriteria criteria = createDetachedCriteria();
criteria.add(Restrictions.eq("some_id", value));
return getHibernateTemplate().findByCriteria(criteria);
public List<SnsUser> getAllSnsUsersByProperty(String prop, Object val){
String query = "from SnsUser su where su." + prop + " =:" + prop;
return executeQuery(query, new String[]{prop}, new Object[]{val});
}
public static void main(String[] args) { //WORKING
String query = "from SnsUser su where su.blessUserId=1";
Session session = Utility.getSessionFactory().openSession();
List l = new SnsUserDaoImpl().getQRes(query);
System.out.println(l);
session.close();
}
public List<E> executeQuery(String queryString, String []param, Object [] val){
//NOT WORKING
return getHibernateTemplate().findByNamedParam(queryString, param, val);
}
这就是我得到的...
Hibernate: select * from bless_aggregation.sns_user this_ left outer join bless_aggregation.sns_authenticator snsauthent2_ on this_.sns_uid=snsauthent2_.sns_uid
where this_.bless_uid=?
Hibernate: select * from bless_aggregation.bless_user blessuser0_ where blessuser0_.bless_uid=?
Hibernate: select * from bless_aggregation.sns_user snsuser0_ left outer join bless_aggregation.sns_authenticator snsauthent1_ on
snsuser0_.sns_uid=snsauthent1_.sns_uid where snsuser0_.bless_uid=?
Hibernate: insert into bless_aggregation.sns_authenticator (key, value, sns_uid) values (?, ?, ?)
1079 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 1064, SQLState: 42000
1079 [main] ERROR org.hibernate.util.JDBCExceptionReporter - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key, value, sns_uid) values (null, null, 1)' at line 1
I am new to Hibernate. I have a problem that when I am trying to run select query say
"from Foo where Foo.some_id=2"
(with Hibernate template) then Hibernate is also tries to insert the records in a table 'Foo2' that has a one-2-one association with the Foo table
Bean Foo
class Foo{
int id;
....
Foo2 foo2;
}
Foo.hbm.xml
...
<one-to-one name="foo2" class="Foo2" property-ref="foo"
constrained="false" cascade="save-update"></one-to-one>
...
Bean Foo2
Class Foo2{
...
private int foo;
...
}
Foo2.hbm.xml
...
<property name="foo" column="foo_id"/>
...
Usage
DetachedCriteria criteria = createDetachedCriteria();
criteria.add(Restrictions.eq("some_id", value));
return getHibernateTemplate().findByCriteria(criteria);
public List<SnsUser> getAllSnsUsersByProperty(String prop, Object val){
String query = "from SnsUser su where su." + prop + " =:" + prop;
return executeQuery(query, new String[]{prop}, new Object[]{val});
}
public static void main(String[] args) { //WORKING
String query = "from SnsUser su where su.blessUserId=1";
Session session = Utility.getSessionFactory().openSession();
List l = new SnsUserDaoImpl().getQRes(query);
System.out.println(l);
session.close();
}
public List<E> executeQuery(String queryString, String []param, Object [] val){
//NOT WORKING
return getHibernateTemplate().findByNamedParam(queryString, param, val);
}
This is what i am getting...
Hibernate: select * from bless_aggregation.sns_user this_ left outer join bless_aggregation.sns_authenticator snsauthent2_ on this_.sns_uid=snsauthent2_.sns_uid
where this_.bless_uid=?
Hibernate: select * from bless_aggregation.bless_user blessuser0_ where blessuser0_.bless_uid=?
Hibernate: select * from bless_aggregation.sns_user snsuser0_ left outer join bless_aggregation.sns_authenticator snsauthent1_ on
snsuser0_.sns_uid=snsauthent1_.sns_uid where snsuser0_.bless_uid=?
Hibernate: insert into bless_aggregation.sns_authenticator (key, value, sns_uid) values (?, ?, ?)
1079 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 1064, SQLState: 42000
1079 [main] ERROR org.hibernate.util.JDBCExceptionReporter - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key, value, sns_uid) values (null, null, 1)' at line 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是,您在会话中有挂起的更改(一些 Foo2 实例等待插入),并且默认情况下,Hibernate 在运行查询之前刷新会话以提供非过时的结果。文档的以下部分对此进行了解释:
因此,正如上面所解释的和代码片段中所示,尝试使用
FlushMode.COMMIT
。请注意,如果您使用
identity
生成器,这将无济于事,Hibernate 将在save
时写入数据库。另请注意,
FlushMode
只是Session
的提示,其行为并未得到严格保证。My guess is that you have pending changes in the session (some Foo2 instances waiting to be inserted) and, by default, Hibernate flushes the session before running a query to give you non stale results. This is explained in the following section of the documentation:
So, as explained above and as shown in the code snippet, try to use
FlushMode.COMMIT
.Note that this won't help if you are using an
identity
generator, Hibernate will write to the database atsave
time.Also note that the
FlushMode
is just an hint for theSession
, the behavior is not strictly guaranteed.