• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Hibernate problem - creating duplicate foreign key index

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my first hibernate app and I have the database schema built and up on mySQL. When I try to insert into the database, hibernate for some reason wants to create the foreign key index. this results in a duplicate foreign key index. Can someone tell me how I configure hibernate to use the foreign key index that already exists in the database?
Thanks in advance ....
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Tech Brat"

Please click on the My Profile link above and change your display name to meet the JavaRanch Naming Policy of using your real first and real last names.

Thanks

Mark
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When mapping your objects if you have a bi-directional relationship, you need to declare one side with inverse="true" so that Hibernate knows that both sides use the same Foreign Key. Also put the inverse="true" on your many side in the one-to-many relationships.

Mark
 
Vijay Chauhan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,
Thanks for the input. But I do have inverse="true" on my mapping. Here are my mapping docs;

<hibernate-mapping>
<class name="Profile" table="profiles">
<id name="id" type="int" column="id" >
<generator class="native"/>
</id>
<property name="clientName">
<column name="client_name" />
</property>
<property name="lastUpdateDate">
<column name="last_update_date" />
</property>
<set name="includedAccounts"
inverse="true"
lazy="false"
cascade="save-update">
<key column="profile_id" not-null="true"/>
<one-to-many class="Account"/>
</set>
<set name="notificationEmails"
inverse="true"
lazy="false"
cascade="save-update">
<key column="profile_id" not-null="true"/>
<one-to-many class="Email"/>
</set>
</class>
</hibernate-mapping>
----------------------------------------------------------------------------------
<hibernate-mapping>
<class name="Account" table="accounts">
<id name="id" type="int" column="id" >
<generator class="native"/>
</id>

<property name="AcctID">
<column name="acct_id" not-null="true"/>
</property>

<many-to-one name="profile"
column="profile_id"
not-null="true"
cascade="save-update"
class="Profile"/>

<set name="reports"
inverse="true"
lazy="true"
cascade="save-update">
<key column="acct_id" not-null="true"/>
<one-to-many class="Report"/>
</set>
</class>
</hibernate-mapping>
----------------------------------------------------------------------------------
<hibernate-mapping>
<class name="Report" table="reports">
<id name="id" type="int" column="id" >
<generator class="native"/>
</id>
<property name="reportDate">
<column name="report_date"/>
</property>
<property name="reportData" type="text">
<column name="report_data"/>
</property>
<many-to-one name="acct"
column="acct_id"
not-null="true"
cascade="save-update"
class="Account"/>
</class>
</hibernate-mapping>
----------------------------------------------------------------------------------
<hibernate-mapping>
<class name="Email" table="emails">
<id name="id" type="int" column="id" >
<generator class="native"/>
</id>
<property name="emailAddress">
<column name="email"/>
</property>
<many-to-one name="profile"
column="profile_id"
not-null="true"
cascade="save-update"
class="Profile"/>
</class>
</hibernate-mapping>
----------------------------------------------------------------------------------
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you repost your mappings, but use the CODE button that is below the Add Reply button, so that the indentation/formatting of the text stays. Right now it is tough to read.

Thanks

Mark
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you do have inverse= true

<set name="includedAccounts"
inverse="true"
lazy="false"
cascade="save-update">


But you have it on the one side. If you read my post above, I stated that you want to put the inverse="true" on the many side. Meaning put it in your Account and Report class mapping not the Profile class mapping like you have, and let us know what are your results.

Mark
 
Vijay Chauhan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There isn't an inverse attribute on the many side. So i can't specify inverse on the many side. So how do I solve my issue ? Thanks in advance.

[ December 05, 2006: Message edited by: Vijay Chauhan ]
 
Vijay Chauhan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There isn't an inverse attribute on the many side. So i can't specify inverse on the many side. So how do I solve my issue ? Thanks in advance.



reposting mapping docs with formatting below ...

 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, add this to your <many-to-one>

insert="false" update="false"

Mark
 
Vijay Chauhan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure? All I'm trying to get hibernate to do is recognize the existing foreignKey index and not create a duplicate foreignKey index. The properties you've asked me to add are defined as follows in the hibernate docs;

http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping.html

5.1.10. many-to-one
update, insert (optional - defaults to true) specifies that the mapped columns should be included in SQL UPDATE and/or INSERT statements. Setting both to false allows a pure "derived" association whose value is initialized from some other property that maps to the same colum(s) or by a trigger or other application.

This does not sound like something one would want to happen since i'm guessing the value is not "derieved".

Please advice. Thanks again.



Originally posted by Mark Spritzler:
OK, add this to your <many-to-one>

insert="false" update="false"

Mark

 
Vijay Chauhan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok figured out the solution!

Just specified the foreignKey index name in the "foreign-key" attribute on the "many-to-one" mapping and it does not create a duplicate index.

Thanks for your input.
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vijay Chauhan:
Are you sure? All I'm trying to get hibernate to do is recognize the existing foreignKey index and not create a duplicate foreignKey index. The properties you've asked me to add are defined as follows in the hibernate docs;

http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping.html

5.1.10. many-to-one
update, insert (optional - defaults to true) specifies that the mapped columns should be included in SQL UPDATE and/or INSERT statements. Setting both to false allows a pure "derived" association whose value is initialized from some other property that maps to the same colum(s) or by a trigger or other application.

This does not sound like something one would want to happen since i'm guessing the value is not "derieved".

Please advice. Thanks again.





It is an alternative to inverse="true", and used when mapping Lists, which you aren't using, but given as a suggestion to see what happens. But I never did see that you didn't have the foreign key mapping missing on that side.

Mark
 
Vijay Chauhan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess you missed this in my first post :
"Can someone tell me how I configure hibernate to use the foreign key index that already exists in the database?"

I figured there must be a way to configure it but just didn't know how .. being a newbie to hibernate.

Thanks for looking into it though.
 
It is no measure of health to be well adjusted to a profoundly sick society. -Krishnamurti Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic