-->
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.  [ 2 posts ] 
Author Message
 Post subject: Inheritance and One-One relationship getting StackOverflow:
PostPosted: Thu Mar 04, 2004 1:59 pm 
Newbie

Joined: Thu Mar 04, 2004 1:38 pm
Posts: 1
Hi,
I'm pretty new to hibernate and just playing around and making some tests. So far - slim but pretty mighty. Congratulation really a good tool.
I have never moved on so fast in testing a new tool and getting quick results.

My Environment: Eclipse, Hypersonic, Ant+XDoclet and of course hibernate 2.1.

The situation:

Superclass Human, Male and Female are extended. Mail and Female have a one to one relationship.
Maybe I'havent understood all, but what is wrong:

Create table human (
name Varchar(128),
type Varchar(128),
partner Varchar(128),
Primary Key (name)
);

/*
* Created on 04.03.2004
*
*/
package test4;

/**
* @hibernate.class
* table="test4"
*
* @hibernate.discriminator
* column="type"
* type="string"
*
* @author kalexm
*
*/
public class Human {

private String name;

/**
* @hibernate.id
* column="name"
* generator-class="assigned"
*/
public String getName() {
return name;
}

/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}

}

/*
* Created on 04.03.2004
*
*/
package test4;

/**
* @hibernate.subclass
* discriminator-value="Male"
*
* @author kalexm
*
* see female for docu (are more eloquent)
*/
public class Male extends Human{

private Female wife;

/**
*
* @hibernate.column
* name="partner"
*
* @hibernate.one-to-one
* class="test4.Female"
* constrained="true"
* cascade="none"
*/
public Female getWife() {
return wife;
}

/**
* @param wife The wife to set.
*/
public void setWife(Female wife) {
this.wife = wife;
wife.setHusband(this);
}

}
/*
* Created on 04.03.2004
*
*/
package test4;

/**
* @hibernate.subclass
* discriminator-value="Female"
*
* @author kalexm
*
* inherited class, mapped like human to the human table, but
* is distincted by the discriminator column (type) and has an additional attribute
* husband. The husband is a self referenced foreign key on the same table, human.
*/
public class Female extends Human {

private Male husband;

/**
*
* @hibernate.column
* name="partner"
*
* @hibernate.one-to-one
* class="test4.Male"
* constrained="true"
* cascade="none"
*
* cascading would mean removing the couple?
*/
public Male getHusband() {
return husband;
}

/**
* @param husband The husband to set.
*/
public void setHusband(Male husband) {
this.husband = husband;
husband.setWife(this);
}

}

/*
* Created on 04.03.2004
*
*/
package test4;

import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;

/**
* @author kalexm
*
* One-One Relationship with inheritance
*/
public class MarriageMaker {

public static void main(String[] args) {
System.out.println("starting my Test4");
addEntry();
}
private static void addEntry() {
SessionFactory sessionFactory = null;
System.out.println("testing add");
try {
sessionFactory = new Configuration()
.configure()
.buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
}
try {
Session session = sessionFactory.openSession();
Male ken = new Male();
ken.setName("Ken");
Female barbie = new Female();
barbie.setName("Barbie");
//sets automaticallly the husband..
ken.setWife(barbie);
session.save(ken);
session.save(barbie);
System.out.println("a new entry was written to the database");
session.flush();
session.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
#######################################
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 1.1//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-1.1.dtd">

<hibernate-mapping>
<class
name="test4.Human"
table="test4"
>

<id
name="name"
column="name"
type="java.lang.String"
>
<generator class="assigned">
</generator>
</id>

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

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Human.xml
containing the additional properties and place it in your merge dir.
-->
<subclass
name="test4.Male"
discriminator-value="Male"
>

<one-to-one
name="wife"
class="test4.Female"
cascade="none"
outer-join="auto"
constrained="true"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Male.xml
containing the additional properties and place it in your merge dir.
-->

</subclass>
<subclass
name="test4.Female"
discriminator-value="Female"
>

<one-to-one
name="husband"
class="test4.Male"
cascade="none"
outer-join="auto"
constrained="true"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Female.xml
containing the additional properties and place it in your merge dir.
-->

</subclass>

</class>

</hibernate-mapping>

#######################################
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>

<session-factory>

<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="hibernate.dialect">net.sf.hibernate.dialect.HSQLDialect</property>
<property name="hibernate.connection.url">jdbc:hsqldb:hsql://127.0.0.1</property>
<property name="hibernate.connection.username">SA</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.connection.pool_size">1</property>

<mapping resource="test1/ManyDatatypes.hbm.xml"/>
<mapping resource="test2/Person.hbm.xml"/>
<mapping resource="test3/Address.hbm.xml"/>
<mapping resource="test4/Human.hbm.xml"/>

</session-factory>

</hibernate-configuration>
#######################################
The result from ant:

Buildfile: C:\Programme\eclipse_3.0M6_SWT_DESIGNER\workspace\Hibernate Tests\build.xml
start_tests:
[java] starting my Test1
[java] testing add
[java] (cfg.Environment 462 ) Hibernate 2.1.2
[java] (cfg.Environment 491 ) hibernate.properties not found
[java] (cfg.Environment 519 ) using CGLIB reflection optimizer
[java] (cfg.Configuration 854 ) configuring from resource: /hibernate.cfg.xml
[java] (cfg.Configuration 826 ) Configuration resource: /hibernate.cfg.xml
[java] (cfg.Configuration 311 ) Mapping resource: test1/ManyDatatypes.hbm.xml
[java] (cfg.Binder 229 ) Mapping class: test1.ManyDatatypes -> test1
[java] (cfg.Configuration 311 ) Mapping resource: test2/Person.hbm.xml
[java] (cfg.Binder 229 ) Mapping class: test2.Person -> test2
[java] (cfg.Configuration 311 ) Mapping resource: test3/Address.hbm.xml
[java] (cfg.Binder 229 ) Mapping class: test3.Address -> test3
[java] (cfg.Configuration 311 ) Mapping resource: test4/Human.hbm.xml
[java] (cfg.Binder 229 ) Mapping class: test4.Human -> test4
[java] (cfg.Binder 169 ) Mapping subclass: test4.Male -> test4
[java] (cfg.Binder 169 ) Mapping subclass: test4.Female -> test4
[java] (cfg.Configuration 1017) Configured SessionFactory: null
[java] (cfg.Configuration 595 ) processing one-to-many association mappings
[java] (cfg.Configuration 604 ) processing one-to-one association property references
[java] (cfg.Configuration 629 ) processing foreign key constraints
[java] (cfg.SettingsFactory 62 ) Use outer join fetching: false
[java] (connection.DriverManagerConnectionProvider 41 ) Using Hibernate built-in connection pool (not for production use!)
[java] (connection.DriverManagerConnectionProvider 42 ) Hibernate connection pool size: 1
[java] (connection.DriverManagerConnectionProvider 71 ) using driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:hsql://127.0.0.1
[java] (connection.DriverManagerConnectionProvider 72 ) connection properties: {user=SA, password=}
[java] (transaction.TransactionManagerLookupFactory 33 ) No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
[java] (cfg.SettingsFactory 102 ) Use scrollable result sets: true
[java] (cfg.SettingsFactory 105 ) Use JDBC3 getGeneratedKeys(): false
[java] (cfg.SettingsFactory 108 ) Optimize cache for minimal puts: false
[java] (cfg.SettingsFactory 117 ) Query language substitutions: {}
[java] (cfg.SettingsFactory 128 ) cache provider: net.sf.ehcache.hibernate.Provider
[java] (cfg.Configuration 1080) instantiating and configuring caches
[java] (impl.SessionFactoryImpl 119 ) building session factory
[java] (util.ReflectHelper 160 ) reflection optimizer disabled for: test4.Male, BulkBeanException: null (property setWife)
[java] (util.ReflectHelper 160 ) reflection optimizer disabled for: test4.Female, BulkBeanException: null (property setHusband)
[java] (impl.SessionFactoryObjectFactory 82 ) no JNDI name configured
[java] java.lang.StackOverflowError
BUILD SUCCESSFUL
Total time: 13 seconds

I think the answer is something like 'read the manual' but a hint would be enough.

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 5:25 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You'can't have a one-to-one between subclasses. one-to-one classes should have the same id. check property-ref attribute.

But I'm not sure this is the real problem

_________________
Emmanuel


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