-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Bug in hibernate core or what?
PostPosted: Fri Nov 21, 2008 12:36 pm 
Newbie

Joined: Tue Jul 12, 2005 2:46 pm
Posts: 13
Location: BS2
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();
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2008 3:32 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Code:
session.saveOrUpdate(travel);


You do not have to call this. Hibernate will automatically track changes you make to objects loaded in the session. However, everything is just kept internally in memory until you tell Hibernate to flush the changes to the database. That is why your DELETE query is failing. Hibernate has not yet pushed the changes from travel.setCar(null); to the database. To make this happen you should call session.flush() just before you execute the DELETE query.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.