Note that there are some explanatory texts on larger screens.

plurals
  1. POPrevent insert while select query in Hibernate
    primarykey
    data
    text
    <p>I am new to Hibernate. I have a problem that when I am trying to run select query say</p> <pre><code>"from Foo where Foo.some_id=2" </code></pre> <p>(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 </p> <p>Bean Foo</p> <pre><code>class Foo{ int id; .... Foo2 foo2; } </code></pre> <p>Foo.hbm.xml</p> <pre><code>... &lt;one-to-one name="foo2" class="Foo2" property-ref="foo" constrained="false" cascade="save-update"&gt;&lt;/one-to-one&gt; ... </code></pre> <p>Bean Foo2</p> <pre><code>Class Foo2{ ... private int foo; ... } </code></pre> <p>Foo2.hbm.xml</p> <pre><code>... &lt;property name="foo" column="foo_id"/&gt; ... </code></pre> <p>Usage </p> <pre><code> DetachedCriteria criteria = createDetachedCriteria(); criteria.add(Restrictions.eq("some_id", value)); return getHibernateTemplate().findByCriteria(criteria); public List&lt;SnsUser&gt; 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&lt;E&gt; executeQuery(String queryString, String []param, Object [] val){ //NOT WORKING return getHibernateTemplate().findByNamedParam(queryString, param, val); } </code></pre> <p>This is what i am getting...</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload