0
如何从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)
相反,我得到这个
信息:在删除处理处理瞬态实体
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>
这does not工作太
session.createQuery("from Modele where model = :mmodel and markaid = :mmarkaid").setParameter("mmodel", u.getModel()).setParameter("mmarkaid", u.getMarki().getId()).executeUpdate();
不连接查询。最好使用Query对象模板。 http://stackoverflow.com/questions/14458986/hibernate-having-difficulty-with-character-in-hql/14459137#14459137 – Taky
@Taky:最好使用查询参数或条件来构建动态查询。 (查询对象模式不解决查询的创建)。 –
@StefanSteinegger:我以为查询对象模式(http://martinfowler.com/eaaCatalog/queryObject.html)定义模式包含标准和查询参数,不是吗? – Taky