0

How do I delete object from Hibernate?

 Session session = HibernateSession.getSessionFactory().openSession();
        org.hibernate.Transaction tx = session.beginTransaction();
        int q = session.createQuery("from Modele where (model='"+u.getModel() +"' and markaid='"+u.getMarki().getId()+"'").executeUpdate();
      //  session.delete(u);

        session.getTransaction().commit();

INFO: Not supported! Use the AST translator...

When using session.delete(u) instead I get this

INFO: handling transient entity in delete processing

CREATE TABLE modele
(
  id serial NOT NULL,
  markaid integer NOT NULL,
  cena numeric(100,2),
  model character varying(32),
  CONSTRAINT k_glwny PRIMARY KEY (id),
  CONSTRAINT obcy FOREIGN KEY (markaid)
      REFERENCES marki (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE CASCADE
)

<hibernate-mapping>
    <class name="bazaMap.Modele" table="modele" schema="public">
        <id name="id" type="int">
            <column name="id" />
            <generator class="identity" />
        </id>
        <many-to-one name="marki" class="bazaMap.Marki" fetch="select">
            <column name="markaid" not-null="true" />
        </many-to-one>
        <property name="cena" type="java.lang.Double">
            <column name="cena" scale="0" />
        </property>
        <property name="model" type="string">
            <column name="model" length="32" />
        </property>
        <set name="wypozyczenias" inverse="true">
            <key>
                <column name="modelid" not-null="true" />
            </key>
            <one-to-many class="bazaMap.Wypozyczenia" />
        </set>
    </class>
</hibernate-mapping>

This doesnt work too

session.createQuery("from Modele where model = :mmodel and markaid = :mmarkaid").setParameter("mmodel", u.getModel()).setParameter("mmarkaid", u.getMarki().getId()).executeUpdate();
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1997553
  • 233
  • 1
  • 2
  • 10
  • 1
    Do not concatenate query. It is better to use Query object patern. http://stackoverflow.com/questions/14458986/hibernate-having-difficulty-with-character-in-hql/14459137#14459137 – Taky Jan 24 '13 at 14:23
  • @Taky: It is better to use query parameters or criteria to build dynamic queries. (The query object pattern doesn't solve the creation of the query). – Stefan Steinegger Jan 24 '13 at 14:28
  • @StefanSteinegger: I thought Query Object Pattern (http://martinfowler.com/eaaCatalog/queryObject.html) definition pattern include criteria and query parameters, isn't it? – Taky Jan 24 '13 at 14:43
  • It wasn't an answer. It is the only advice for Hibernate using. I am confused by your code example. Why delete method with commit is not appropriate for you? In general way you can use http://docs.jboss.org/hibernate/orm/3.3/api/org/hibernate/Session.html or HQL delete: http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/batch.html#batch-direct – Taky Jan 24 '13 at 15:22
  • but the problem is that, every delete query I make with createQuery, i get the " INFO: Not supported! Use the AST translator... " error. – user1997553 Jan 24 '13 at 15:36
  • @Taky: the pattern only says that the query is built by the query object, but not that it should not concatenate strings to do this. This is on a different level. Using query parameters is a best practice and doesn't relate to whether you use the query pattern or not. – Stefan Steinegger Jan 25 '13 at 14:45

1 Answers1

0

I believe that your HQL is missing the DELETE keyword which is causing the AST error (basically an HQL syntax error.) It should be:

DELETE from Modele where (model='"+u.getModel() +"' and markaid='"+u.getMarki().getId()+"'"

If you want to use the delete method (which is actually deprecated in hibernate 3 in favor of using HQL) you have to make sure that you load the entity in the same session your are deleting it from before you delete it.

I generally use the HQL style delete, although I would recommend using named parameters instead of string concatenation.

Mike
  • 165
  • 5