如何从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();
信息:不支持!使用AST翻译......
使用session.delete(u)
代替我
INFO:在删除处理中处理瞬态实体
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>
这也不起作用
session.createQuery("from Modele where model = :mmodel and markaid = :mmarkaid").setParameter("mmodel", u.getModel()).setParameter("mmarkaid", u.getMarki().getId()).executeUpdate();
答案 0 :(得分:0)
我相信您的HQL缺少导致AST错误的DELETE关键字(基本上是HQL语法错误。)它应该是:
DELETE from Modele where (model='"+u.getModel() +"' and markaid='"+u.getMarki().getId()+"'"
如果你想使用delete方法(在hibernate 3中实际上不赞成使用HQL),你必须确保在删除它之前将实体加载到你删除它的同一个会话中。
我通常使用HQL样式删除,但我建议使用命名参数而不是字符串连接。