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