-->
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: Unique-Key over 3 columns from 2 objects
PostPosted: Thu Jun 16, 2005 10:58 am 
Newbie

Joined: Sun Nov 07, 2004 6:58 am
Posts: 8
Location: HH Germany
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.4

Mapping documents:
Code:
hibernate-mapping auto-import="true" package="de.mk.srb.model">

   <class name="Stock" table="stocks" lazy="false">
      <id name="id" column="id" unsaved-value="-1">
         <generator class="identity" />
      </id>
      <property name="name" column="name" lazy="false" not-null="true"/>
      <property name="isin" unique="true" column="isin" lazy="false" not-null="true"/>
      <property name="symbol" unique="true" column="symbol" lazy="false" not-null="true"/>
      <property name="wkn" unique="true" column="wkn" lazy="false"/>
      <set name="indexeInternal"  cascade="all" table="stock_indexe" lazy="false">
         <key column="stock_id"/>
         <many-to-many column="index_id" class="Index"/>
      </set>
      <set name="pricesInternal" cascade="all" lazy="false">
         <key>
             <column name="stock_id" unique-key="StockPrice" not-null="true"/>
         </key>
         <one-to-many class="StockPrice"/>
      </set>
            
   </class>

   <class name="Index" table="indexe">
      <id name="id" column="id" unsaved-value="-1">
         <generator class="identity" />
      </id>
      <property name="name" unique="true" column="name" not-null="true"/>
      <many-to-one name="country" column="country_id" class="Country" cascade="all" not-null="true"/>
   </class>
   
   <class name="StockPrice" table="prices">
      <id name="id" column="id" unsaved-value="-1">
         <generator class="identity" />
      </id>
      <!-- properties name="pricekey" unique="true" unique-key="StockPrice"-->
         <property name="date">
             <column name="date" not-null="true" unique-key="StockPrice"/>
          </property>
         <property name="price">
            <column name="price" not-null="true" unique-key="StockPrice"/>
         </property>
         <!-- many-to-one name="stock" column="stock_id" class="Stock" cascade="all" not-null="true"/-->
      <!-- /properties-->
   </class>



Code between sessionFactory.openSession() and session.close():
done by the Spring Hibernate Template


Full stack trace of any exception that occurs:
just SQL constraint failure
nested exception is java.sql.SQLException: Cannot add or update a child row: a foreign key constraint fails [See nested exception: org.springframework.dao.DataIntegrityViolationException: (Hibernate operation: could not insert: [de.mk.srb.model.StockPrice]): data integrity violated by SQL 'insert into prices (date, price) values (?, ?)'; nested exception is java.sql.SQLException: Cannot add or update a child row: a foreign key constraint fails]


Name and version of the database you are using: MySQL 4.1

The generated SQL (show_sql=true):
insert into prices (date, price) values (?, ?)


Debug level Hibernate log excerpt:


the problem is, that I need a unique-key over the three columns, but hibernate first insert the only the price and date column and then hibernate will update this column with the stock_id.

if I say that the stock_id column can't be null, hibernate can't insert the price (Object).

If I say that the stock_id column can be null, hibernate can insert the price(Object) but because the unique key over the three columns the update of the stock_id column will result in a another database row. (one with stock_id = null and the same with the correct stock_id)

My question is there a way to configure hibernate to insert the stock_id when the to other columns (date and price) will be inserted (this shuold be possible because the stock_id is allready known) or to tell hibernate that the updating of the stock_id column not result in a new row ???

Thanks.

ys Gideon


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 16, 2005 11:32 am 
Newbie

Joined: Sun Nov 07, 2004 6:58 am
Posts: 8
Location: HH Germany
hi,

I found the solution for my problen, just adding the nut-null="true" attribute just for the key element and not to colmun.

this works how I will but now: hibernate does the following:

Code:
Hibernate: insert into prices (date, price, stock_id) values (?, ?, ?)
Hibernate: update stocks set name=?, isin=?, symbol=?, wkn=? where id=?
Hibernate: update prices set date=?, price=? where id=?
Hibernate: update prices set stock_id=? where id=?


did this mean, that hibernate update stocks because it does not know if the oject details have ben changed will be ok but why hibernate updates the price objects two times again?? there can't be any change and also the key (stock_id) cant be changed in that time, so why ?? this is just a performace break.

thanks.

ys. Gideon


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.