|
From: <leg...@at...> - 2003-10-16 15:04:54
|
The following comment has been added to this issue:
Author: Andrey Volozin
Created: Thu, 16 Oct 2003 10:01 AM
Body:
sorry, here is a correction for class D:
public class D extends A {
private String f4;
private Collection lnkB;
private Collection lnkC;
public String getF4() { return f4; }
public void setF4(String f4) { this.f4 = f4; }
public Collection getLnkB() { return lnkB; }
public void setLnkB(String lnkB) { this.lnkB = lnkB; }
public Collection getLnkC() { return lnkC; }
public void setLnkC(Collection lnkC) { this.lnkC = lnkC; }
}
---------------------------------------------------------------------
View the issue:
http://opensource.atlassian.com/projects/hibernate/secure/ViewIssue.jspa?key=HB-406
Here is an overview of the issue:
---------------------------------------------------------------------
Key: HB-406
Summary: graph loading breaks on complex hierarchies
Type: Bug
Status: Unassigned
Priority: Major
Project: Hibernate2
Components:
core
Versions:
2.0.3
Assignee:
Reporter: Andrey Volozin
Created: Thu, 16 Oct 2003 9:57 AM
Updated: Thu, 16 Oct 2003 9:57 AM
Environment: Pentium IV 2.40 GHz, Windows XP, JDK 1.3
Description:
I have a relatively complex hierarchy of objects:
A is common superclass
B, C, D are subclasses
D incapsulates collections of B and C
here is my hbm.xml file:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="A">
<id name="id">
<generator class="hilo">
<param name="table">hi_value</param>
<param name="column">next_value</param>
<param name="max_lo">100</param>
</generator>
</id>
<discriminator column="class_name"/>
<property name="f1"/>
<subclass name="B">
<property name="f2"/>
</subclass>
<subclass name="C">
<property name="f3"/>
</subclass>
<subclass name="D">
<set name="lnkB" cascade="all">
<key column="d_id"/>
<one-to-many class="B"/>
</set>
<set name="lnkC" cascade="all">
<key column="d_id"/>
<one-to-many class="C"/>
</set>
<property name="f4"/>
</subclass>
</class>
</hibernate-mapping>
here are the classes:
public abstract class A {
private Long id;
private String f1;
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getF1() { return f1; }
public void setF1(String f1) { this.f1 = f1; }
}
public class B extends A {
private String f2;
public String getF2() { return f2; }
public void setF2(String f2) { this.f2 = f2; }
}
public class C extends A {
private String f3;
public String getF3() { return f3; }
public void setF3(String f3) { this.f3 = f3; }
}
public class D extends A {
private String f4;
private Collection lnkB;
private Collection lnkC;
public String getF4() { return f4; }
public void setF4(String f4) { this.f4 = f4; }
public Collection getLnkB() { return lnkB; }
public void setLnkB(String lnkB) { this.lnkB = lnkB; }
public String getLnkC() { return lnkC; }
public void setLnkC(String lnkC) { this.lnkC = lnkC; }
}
I persisted data to the database using the following code:
public class TestMain {
static Session session;
static SessionFactory sessionFactory;
public static Collection createB() throws HibernateException, SQLException {
Collection results = new HashSet();
B b = new B();
b.setF2("foo");
results.add(b);
b = new B();
b.setF2("bar");
results.add(b);
return results;
}
public static Collection createC() throws HibernateException, SQLException {
Collection results = new HashSet();
C c = new C();
c.setF3("foo");
results.add(c);
c = new C();
c.setF3("bar");
results.add(c);
return results;
}
public static D createD(Collection b, Collection c) throws HibernateException, SQLException {
D d = new D();
d.setF1("foo");
d.setLnkB(b);
d.setLnkC(c);
session.save (d);
System.out.println ("Persisting D");
return d;
}
public static void main(String[] args) {
D d;
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
if (sessionFactory == null) { System.out.println ("null session factory returned"); return; }
System.out.println ("Got Session Factory");
session = sessionFactory.openSession();
Collection b = createB();
Collection c = createC();
d = createD(b, c);
session.flush();
session.connection().commit();
System.out.println ("Committing");
session.close();
} catch (MappingException e){
System.out.println (e);
} catch (HibernateException e) {
System.out.println(e);
}
catch (Throwable e) {
System.out.println (e);
e.printStackTrace();
}
}
}
then I started my application again but instead of persistence I did retrieval (I looked up id of D instance in the database):
D d = (D) session.load(D.class, new Long(1615));
But it failed with the message:
Object with id: 1618 was not of the specified subclass: C (loaded object was of wrong class)
Indeed, the object with id 1618 had "B" in the discriminator field and database record corresponded to B instance.
---------------------------------------------------------------------
JIRA INFORMATION:
This message is automatically generated by JIRA.
If you think it was sent incorrectly contact one of the administrators:
http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
If you want more information on JIRA, or have a bug to report see:
http://www.atlassian.com/software/jira
|