aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate problem - creating duplicate foreign key index Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate problem - creating duplicate foreign key index" Watch "Hibernate problem - creating duplicate foreign key index" New topic
Author

Hibernate problem - creating duplicate foreign key index

Vijay Chauhan
Greenhorn

Joined: Nov 28, 2006
Posts: 11
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 ....
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

"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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

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

Joined: Nov 28, 2006
Posts: 11
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
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

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
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

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

Joined: Nov 28, 2006
Posts: 11
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

Joined: Nov 28, 2006
Posts: 11
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
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

OK, add this to your <many-to-one>

insert="false" update="false"

Mark
Vijay Chauhan
Greenhorn

Joined: Nov 28, 2006
Posts: 11
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

Joined: Nov 28, 2006
Posts: 11
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
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

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

Joined: Nov 28, 2006
Posts: 11
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Hibernate problem - creating duplicate foreign key index