-->
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.  [ 7 posts ] 
Author Message
 Post subject: Mapping with "Mixin" generates duplicate FK DDL.
PostPosted: Tue Mar 21, 2006 5:35 pm 
Regular
Regular

Joined: Tue Mar 21, 2006 11:01 am
Posts: 65
My mapping is trying to describe the following inheritance hierarchy

interface FeatureSetting
interface Selective

class SimpleCallForwardSetting
implements FeatureSetting

class SelectiveCallForwardSetting
extends SimpleCallForwardSetting
implements Selective

class SwitchedSetting
implements FeatureSetting

class SelectiveSwitchedSetting
extends SwitchedSetting
implements Selective

The implementation of Selective (a mixin) takes the form of creating a one-to-many mapping of FeatureListMembers (implemented as a Set). The two classes which implement Selective have the extra Set , the other two classes don't. The Selective interface itself is not mentioned in the Hibernate mapping (see below).

I am trying to use the "Table Per Class Hierarchy" strategy, except, of course, for the Feature List Members themselves, which are in a related table.

This plan fails in the SQL Generation phase with a database error as follows:

Mar 21, 2006 2:48:07 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
SEVERE: Unsuccessful: alter table feature_list_member add constraint FKF123A19283A831E7 foreign key (feature_setting_id) references feature_setting
Mar 21, 2006 2:48:07 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
SEVERE: ORA-02275: such a referential constraint already exists in the table

This error makes perfect sense. Hibernate is trying to generate two instances of the same foreign key, one for each of the classes. (see generated DDL below).

My question is whether there's a better way to model this class hierarchy in Hibernate. I could, I suppose, keep the lists belonging to each hierarchical class in a different table, since there's no particular reason to lump them together. But is this necessary?

Hibernate version: 3.0.5

Mapping documents:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="blah.blah">
<class name="FeatureSetting" table="feature_setting">
<id name="id" type="long" column="feature_setting_id" >
<generator class="seqhilo">
<param name="sequence">feature_setting_seq</param>
<param name="max_lo">100</param>
</generator>
</id>

<discriminator
column="setting_type"
type="string"/>


<many-to-one name="parentPkg"
column="feature_package_id"
not-null="true"/>


<subclass name="SimpleCallForwardSetting"
discriminator-value="forward">
<property name="featureName">
<column name="feature_name" not-null="true"/>
</property>
<property name="destination" not-null="true"/>
<property name="ringCount" column="ring_count"/>
<property name="voiceMail" column="is_voice_mail"/>
</subclass>

<subclass name="SelectiveCallForwardSetting"
discriminator-value="selective-forward">
<property name="featureName">
<column name="feature_name" not-null="true"/>
</property>
<property name="destination" not-null="true"/>
<property name="ringCount" column="ring_count"/>
<property name="voiceMail" column="is_voice_mail"/>
<set name="callListMembers" table="feature_list_member">
<key column="feature_setting_id"/>
<one-to-many class="FeatureListMember" />
</set>
</subclass>

<subclass name="SwitchedSetting"
discriminator-value="switch">
<property name="featureName">
<column name="feature_name" not-null="true"/>
</property>
<property name="on" column="is_on"/>
</subclass>

<subclass name="SelectiveSwitchedSetting"
discriminator-value="selective">
<property name="featureName">
<column name="feature_name" not-null="true"/>
</property>
<property name="on" column="is_on"/>
<set name="listMembers" table="feature_list_member">
<key column="feature_setting_id"/>
<one-to-many class="FeatureListMember" />
</set>
</subclass>


</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close(): N/A, problem is in Generation phase

Full stack trace of any exception that occurs: No exception

Name and version of the database you are using: Oracle 9.2

The generated SQL (show_sql=true):

drop table feature_list_member cascade constraints;
drop table feature_package cascade constraints;
drop table feature_setting cascade constraints;
drop sequence feature_list_member_seq;
drop sequence feature_package_seq;
drop sequence feature_setting_seq;

create table feature_list_member (
feature_list_member_id number(19,0) not null,
rawTn varchar2(255) not null,
first_name varchar2(255),
last_name varchar2(255),
feature_setting_id number(19,0),
primary key (feature_list_member_id)
);
create table feature_package (
feature_package_id number(19,0) not null,
voip_account_id varchar2(255) not null,
sdp_account_id varchar2(255) not null,
primary key (feature_package_id)
);
create table feature_setting (
feature_setting_id number(19,0) not null,
setting_type varchar2(255) not null,
feature_package_id number(19,0) not null,
feature_name varchar2(255) not null,
destination varchar2(255) not null,
ring_count number(10,0),
is_voice_mail number(1,0),
is_on number(1,0),
primary key (feature_setting_id)
);
alter table feature_list_member
add constraint FKF123A1922892E865
foreign key (feature_setting_id)
references feature_setting;
alter table feature_list_member
add constraint FKF123A19283A831E7
foreign key (feature_setting_id)
references feature_setting;
alter table feature_setting
add constraint FK1D39DA0778E17A5E
foreign key (feature_package_id)
references feature_package;

create sequence feature_list_member_seq;
create sequence feature_package_seq;
create sequence feature_setting_seq;


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 21, 2006 6:01 pm 
Regular
Regular

Joined: Tue Mar 21, 2006 11:01 am
Posts: 65
Ooh, could <import> help me? Suppose I imported my FeatureListMember class under a different name (to fool Hibernate). Then I could persist into two separate tables (not a real problem) without having to maintain two classes that were duplicates of each other.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 21, 2006 6:18 pm 
Regular
Regular

Joined: Tue Mar 21, 2006 11:01 am
Posts: 65
stevecoh2 wrote:
Ooh, could <import> help me? Suppose I imported my FeatureListMember class under a different name (to fool Hibernate). Then I could persist into two separate tables (not a real problem) without having to maintain two classes that were duplicates of each other.


Naah, that would be a hack and probably a bad one. That's not what import is for.

All I REALLY want is to be able to do is to place two one-to-many associations from two different subclasses stored in one table to another class. I'd like there to be a way to tell Hibernate "don't create the foreign key DDL twice because only one is needed or allowed".


Top
 Profile  
 
 Post subject: Re: Mapping with "Mixin" generates duplicate FK DDL.
PostPosted: Mon Jun 22, 2009 5:07 pm 
Newbie

Joined: Mon Jun 22, 2009 3:30 pm
Posts: 1
Hi,

I wonder if you get raound the duplicate FK DDL. I have the same problem, try to figure out how to solve ...

Thanks,


Top
 Profile  
 
 Post subject: Re: Mapping with "Mixin" generates duplicate FK DDL.
PostPosted: Tue Jun 23, 2009 9:02 am 
Regular
Regular

Joined: Tue Mar 21, 2006 11:01 am
Posts: 65
lpan wrote:
Hi,

I wonder if you get raound the duplicate FK DDL. I have the same problem, try to figure out how to solve ...

Thanks,


Well, you're responding to a post that's three years old, and I have moved on from this project, but let me try to dig up as best I can from memory.

The answer, as best I can recall, is NO. I had to manually alter the DDL to remove the second foreign key statement. I'm a little vague here because I also used to use the generated DDL as the basis for a battery of JUnit tests that would run against the generated schema using SchemaExport. I don't quite remember how I managed that feat unless the error was non-fatal to SchemaExport, because I know the JUnit battery worked in spite of this problem.

Anyway, thanks for tickling my memory cells.


Top
 Profile  
 
 Post subject: Re: Mapping with "Mixin" generates duplicate FK DDL.
PostPosted: Mon Jun 29, 2009 11:14 am 
Newbie

Joined: Fri Jun 26, 2009 7:31 pm
Posts: 5
hi,

I had the same problem

Inspect your hibernate.cfg.xml File, and if you find <property name="hbm2ddl.auto">update</property> you must delete it or comment it to avoid regenerating the same DDL code each time you connect to the database.

Hope it works for you


Top
 Profile  
 
 Post subject: Re: Mapping with "Mixin" generates duplicate FK DDL.
PostPosted: Mon Jun 29, 2009 11:18 am 
Regular
Regular

Joined: Tue Mar 21, 2006 11:01 am
Posts: 65
karim_sousse wrote:
hi,

I had the same problem

Inspect your hibernate.cfg.xml File, and if you find <property name="hbm2ddl.auto">update</property> you must delete it or comment it to avoid regenerating the same DDL code each time you connect to the database.

Hope it works for you


Please read the original post again. This is not the problem I or the other commenter was having. Of course I don't regenerate DDL everytime I connect to the database. The problem was that due to the particular class structure, incorrect DDL was being generated.


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