Look the MainTest Class, note the delete.
I Use query, no session.delete(...) and this form i have problens
It´s bug in core hibernate or what?
How can i use Query to delete without have problems as this?
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
Hibernate core 3.3.1GA
Mapping documents:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.entity">
<class name="Car">
<id type="long" column="idCar" name="idCar" access="property" unsaved-value="undefined">
<generator class="native">
<param name="sequence">seqPkIdCar</param>
</generator>
</id>
<property name="description" length="15"/>
<bag name="travelList" inverse="true" access="property" >
<key column="fkCar" />
<one-to-many class="Travel" />
</bag>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.entity">
<class name="Travel">
<id type="long" column="idTravel" name="idTravel" access="property" unsaved-value="undefined">
<generator class="native">
<param name="sequence">seqPkIdTravel</param>
</generator>
</id>
<property name="description" length="15" />
<many-to-one name="car" column="fkCar" class="Car" />
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.username">wssiv</property>
<property name="hibernate.connection.password">18012001</property>
<property name="hibernate.connection.url">jdbc:postgresql://192.168.10.15/sim3gt</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Mapping -->
<mapping resource="com/entity/Car.hbm.xml" />
<mapping resource="com/entity/Travel.hbm.xml" />
</session-factory>
</hibernate-configuration>
Full stack trace of any exception that occurs:
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not execute update query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:107)
at org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:419)
at org.hibernate.engine.query.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:283)
at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:1168)
at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:117)
at com.MainTest.main(MainTest.java:54)
Caused by: org.postgresql.util.PSQLException: ERROR: update or delete on table "car" violates foreign key constraint "fk95cb6b3a78a8fea5" on table "travel"
Detalhe: Key (idcar)=(1) is still referenced from table "travel".
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1548)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1316)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:191)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:452)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:351)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:305)
at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:98)
... 5 more
public class MainTest {
/**
* @param args
*/
public static void main(String[] args) {
Configuration cfg = new Configuration().configure();
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
// Find Car
Criteria critCar = session.createCriteria(Car.class);
critCar.add(Restrictions.eq("idCar", new Long(1)));
Car car = (Car) critCar.uniqueResult();
// unlink travel
Criteria critTravel = session.createCriteria(Travel.class);
Criteria crit = critTravel.createAlias("car", "car");
crit.add(Restrictions.eq("car", car));
List lst = crit.list();
for (Iterator iterator = lst.iterator(); iterator.hasNext();) {
Travel travel = (Travel) iterator.next();
travel.setCar(null);
session.saveOrUpdate(travel);
}
// Here ... Probleman
// delete car
StringBuilder sb = new StringBuilder("DELETE FROM ");
sb.append(car.getClass().getCanonicalName());
sb.append(" AS DEL WHERE DEL.id");
sb.append(car.getClass().getSimpleName());
sb.append(" = ");
sb.append(car.getIdCar());
Query query = session.createQuery(sb.toString());
int row = query.executeUpdate();
session.getTransaction().commit();
}
}