-->
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.  [ 6 posts ] 
Author Message
 Post subject: Is this a hack, or is there another solution?
PostPosted: Tue Oct 18, 2005 9:08 am 
Newbie

Joined: Tue Sep 27, 2005 7:30 am
Posts: 12
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

I have a scenario where I need a one to many relationship. I'm using a bidirectional relationship for this. The problem is that I need a light weight object from the inverse object. This object just contains the id(and some other minimal attributes). I could not find a valid solution to this so this is what I've done. Please let me know if this will work, as it seems to be working for basic usage thus far.
I'm using the table per sub class approach, and prefer using that approach although it may not look useful at the moment but I've removed quite a few properties.

If I don't explicitly reference the foreign key names, it creates an extra foreign key which doesn't make sense.

Hibernate version:
Hibernate 3.0.5

Mapping documents:

Code:
<class name="za.co.AccountObjectReference" table="ACCOUNT" lazy="false">
   <id name="objectId" column="OID" />
</class>
<class name="za.co.Account" table="ACCOUNT" lazy="false">
   <id name="objectId" column="OID">
      <generator class="identity" />
   </id>
   <property name="name" column="NAME" update="true" />

   <set name="accountRelationships" inverse="true" cascade="all-delete-orphan" lazy="false">
      <key column="ACCOUNT_ID" not-null="true" foreign-key="accRelFk1" />
      <one-to-many class="za.co.AccountRelationship" />
   </set>
   
   <joined-subclass extends="za.co.Account" name="za.co.MonetaryAccount" table="MONETARYACCOUNT" >
      <key column="OID"/>
      <property name="accountType" column="ACCOUNTTYPE" type="integer" update="true" />
   </joined-subclass>
   
</class>
<class name="za.co.AccountRelationship" table="ACCOUNT_RELATIONSHIP" lazy="false">
   <id name="objectId" column="ID">
      <generator class="identity" />
   </id>
   <many-to-one name="account" class="za.co.AccountObjectReference" column="ACCOUNT_ID" not-null="true" foreign-key="accRelFk1" />
   <many-to-one name="relatedAccount" class="za.co.AccountObjectReference" column="RELATED_ACCOUNT" not-null="true" foreign-key="accRelFk2" />
</class>


Code:
public class AccountRelationship {
   private long objectId;
   private AccountObjectReference account;
   //private Account account; This is the normal way, but i don't want a full object, i just need the id
   private AccountObjectReference relatedAccount;
   //private Account relatedAccount;
}

public class Account extends AccountObjectReference {
   private String name;
   private Set accountRelationships = new HashSet();
}

public class AccountObjectReference {
   private long objectId;
}

public class MonetaryAccount extends Account {
   private Integer accountType;
}



Code between sessionFactory.openSession() and session.close():
N/A
Full stack trace of any exception that occurs:
N/A
Name and version of the database you are using:
MS SQL Server 2000
The generated SQL (show_sql=true):

[schemaexport] alter table ACCOUNT_RELATIONSHIP drop constraint accRelFk2;
[schemaexport] alter table ACCOUNT_RELATIONSHIP drop constraint accRelFk1;
[schemaexport] alter table ACCOUNT_RELATIONSHIP drop constraint accRelFk1;
[schemaexport] drop table ACCOUNT;
[schemaexport] create table ACCOUNT (
[schemaexport] OID numeric(19,0) identity not null,
[schemaexport] NAME varchar(255) null,
[schemaexport] primary key (OID)
[schemaexport] );
[schemaexport] create table MONETARYACCOUNT (
[schemaexport] OID numeric(19,0) not null,
[schemaexport] ACCOUNTTYPE int null,
[schemaexport] primary key (OID)
[schemaexport] );
[schemaexport] create table ACCOUNT_RELATIONSHIP (
[schemaexport] ID numeric(19,0) identity not null,
[schemaexport] ACCOUNT_ID numeric(19,0) not null,
[schemaexport] RELATED_ACCOUNT numeric(19,0) not null,
[schemaexport] primary key (ID)
[schemaexport] );
[schemaexport] alter table ACCOUNT_RELATIONSHIP
[schemaexport] add constraint accRelFk2
[schemaexport] foreign key (RELATED_ACCOUNT)
[schemaexport] references ACCOUNT;
[schemaexport] alter table ACCOUNT_RELATIONSHIP
[schemaexport] add constraint accRelFk1
[schemaexport] foreign key (ACCOUNT_ID)
[schemaexport] references ACCOUNT;
[schemaexport] alter table ACCOUNT_RELATIONSHIP
[schemaexport] add constraint accRelFk1
[schemaexport] foreign key (ACCOUNT_ID)
[schemaexport] references ACCOUNT;
[schemaexport] 12:59:19,786 ERROR SchemaExport:167 - Unsuccessful: alter table ACCOUNT_RELATIONSHIP add constraint accRelFk1 foreign key (ACCOUNT_ID) references ACCOUNT
[schemaexport] 12:59:19,802 ERROR SchemaExport:168 - There is already an object named 'accRelFk1' in the database.
[schemaexport] alter table MONETARYACCOUNT
[schemaexport] add constraint FKDAC159A02B8A55E
[schemaexport] foreign key (OID)
[schemaexport] references ACCOUNT;
[schemaexport] 12:59:19,802 INFO SchemaExport:173 - schema export complete

Debug level Hibernate log excerpt:
N/A


Thanks
Naeem


Top
 Profile  
 
 Post subject: Fellow Hibernate Experts
PostPosted: Fri Oct 21, 2005 10:30 am 
Newbie

Joined: Tue Sep 27, 2005 7:30 am
Posts: 12
Do I need to provide more information?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 10:51 am 
Beginner
Beginner

Joined: Tue Aug 23, 2005 3:52 pm
Posts: 26
It looks like you'll get in to trouble by representing AccountObjectReference as an entity object.

You have to make a choice:

1) Have a set of entity objects of type Account.
Then you cans cascade on persistance actions, but you will have a reference to entire account object.

2) Have a set of components or value types.
You can strore only ids but prersistance will not be cascaded.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 24, 2005 8:36 am 
Newbie

Joined: Tue Sep 27, 2005 7:30 am
Posts: 12
Option 1 is not the route i'd want to go.

My concern is the error when hibernate creates the sql

[schemaexport] 12:59:19,786 ERROR SchemaExport:167 - Unsuccessful: alter table ACCOUNT_RELATIONSHIP add constraint accRelFk1 foreign key (ACCOUNT_ID) references ACCOUNT
[schemaexport] 12:59:19,802 ERROR SchemaExport:168 - There is already an object named 'accRelFk1' in the database.

I need to know how this will impact Hibernate. At the moment I haven't pick up any problems but I'm still in early days of development.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 24, 2005 8:39 am 
Regular
Regular

Joined: Mon Aug 29, 2005 9:46 am
Posts: 102
You're trying to add a constraint that already exists. Why you wanna add it then? Or why don't you drop it?

_________________
Don't forget to rate if the post helped!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 25, 2005 5:10 am 
Newbie

Joined: Tue Sep 27, 2005 7:30 am
Posts: 12
Hi

I know what the problem means, but I had to explicitly name the foreign keys on both mapping files, otherwise Hibernate creates a duplicate foreign key contraint.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.