-->
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: Inconsistent result of @OnToMany between Parent and Childs
PostPosted: Sun Jan 08, 2006 11:20 pm 
Newbie

Joined: Wed Nov 02, 2005 10:49 pm
Posts: 12
Hibernate version:
Hibernate Core 3.1 + Hibernate Annotation 3.1 beta 7

Mapping documents:

SQL
Code:
CREATE TABLE [dbo].[CHILD_TABLE] (
   [CHILD_ID] [char](2) COLLATE Chinese_Taiwan_Stroke_CI_AS NOT NULL ,
   [PARENT_ID] [char] (2) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[PARENT_TABLE] (
   [PARENT_ID] [char] (2) COLLATE Chinese_Taiwan_Stroke_CI_AS NOT NULL
) ON [PRIMARY]
GO


PARENT_TABLE sample datas
Code:
PARENT_ID
==================================
1


CHILD_TABLE sample datas
Code:
CHILD_ID       PARENT_ID
==================================
1              1
2              1
3              1


hibernate.cfg.xml
Code:
<hibernate-configuration>
    <session-factory>
        <property name="show_sql">true</property>
        <property name="hibernate.connection.url"><![CDATA[jdbc:microsoft:sqlserver://192.168.11.50:1433;DatabaseName=Leader]]></property>
        <property name="hibernate.connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
        <property name="hibernate.connection.username">jdbc</property>
        <property name="hibernate.connection.password">jdbc</property>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>

        <mapping class="Parent" />
        <mapping class="Child" />
    </session-factory>
</hibernate-configuration>


Parent.java
Code:
@Entity(access = AccessType.FIELD)
@Table(name = "PARENT_TABLE")
public class Parent implements Serializable {
    @Id
    @Column(name = "PARENT_ID")
    private String id;
   
    @OneToMany(mappedBy = "parent")
    private Set<Child> childs;
   
    public String getId() {
        return id;
    }
   
    public Set<Child> getChilds() {
        return childs;
    }
}


Child.java
Code:
@Entity(access = AccessType.FIELD)
@Table(name = "CHILD_TABLE")
public class Child implements Serializable {
    @Id
    @Column(name = "CHILD_ID")
    private String id;
   
    @ManyToOne
    @JoinColumn(name = "PARENT_ID")
    private Parent parent;
   
    public String getId() {
        return id;
    }
   
    public Parent getParent() {
        return parent;
    }
}


Code between sessionFactory.openSession() and session.close():

Code:
01 Child child = (Child)session1.get(Child.class, "1");
02 System.out.println("I have a parent, his ID is " + child.getParent().getId() + ", and " + child.getParent().getChilds().size() + " borthers and sisters.");
03 Parent childParent = (Parent)session1.get(Parent.class, "1");
04 System.out.println("and, I am happy to know he really have only " + childParent.getChilds().size() + " child?");
05         
06 Parent parent = (Parent)session2.get(Parent.class, "1");
07 System.out.println("After all, my parent says himself that he have " + parent.getChilds().size() + " childs!!");


Full stack trace of any exception that occurs:

Name and version of the database you are using:
Database: Microsoft SQL Server 2000 SP4
JDBC Driver: Microsoft JDBC Driver SP3

The generated SQL (show_sql=true):


Hibernate: select child0_.CHILD_ID as CHILD1_1_1_, child0_.PARENT_ID as PARENT2_1_1_, parent1_.PARENT_ID as PARENT1_0_0_ from CHILD_TABLE child0_ left outer join PARENT_TABLE parent1_ on child0_.PARENT_ID=parent1_.PARENT_ID where child0_.CHILD_ID=?
Hibernate: select childs0_.PARENT_ID as PARENT2_1_, childs0_.CHILD_ID as CHILD1_1_, childs0_.CHILD_ID as CHILD1_1_0_, childs0_.PARENT_ID as PARENT2_1_0_ from CHILD_TABLE childs0_ where childs0_.PARENT_ID=?
I have a parent, his ID is 1 , and 3 borthers and sisters.
Hibernate: select parent0_.PARENT_ID as PARENT1_0_0_ from PARENT_TABLE parent0_ where parent0_.PARENT_ID=?
Hibernate: select childs0_.PARENT_ID as PARENT2_1_, childs0_.CHILD_ID as CHILD1_1_, childs0_.CHILD_ID as CHILD1_1_0_, childs0_.PARENT_ID as PARENT2_1_0_ from CHILD_TABLE childs0_ where childs0_.PARENT_ID=?
and, I am happy to know he really have only 0 child?
Hibernate: select parent0_.PARENT_ID as PARENT1_0_0_ from PARENT_TABLE parent0_ where parent0_.PARENT_ID=?
Hibernate: select childs0_.PARENT_ID as PARENT2_1_, childs0_.CHILD_ID as CHILD1_1_, childs0_.CHILD_ID as CHILD1_1_0_, childs0_.PARENT_ID as PARENT2_1_0_ from CHILD_TABLE childs0_ where childs0_.PARENT_ID=?
Hibernate: select parent0_.PARENT_ID as PARENT1_0_0_ from PARENT_TABLE parent0_ where parent0_.PARENT_ID=?
After all, my parent says himself that he have 0 childs!!


Debug level Hibernate log excerpt:

Some questions as following:
1. Why the result of child.getParent().getChilds().size() (as row 2) and new loaded parent childParent.getChilds().size()(as row 4), parent.getChilds().size()(as row 7) so different and inconsistent
2. I found this inconsistent exists when:
a) using SQL Server 2000 but not MySQL
b) using any JDBC driver, includs jTDS 1.2, Microsoft JDBC dirver SP3...
c) using VARCHAR or CHAR as column type, but not numeric ones

I think this may be the inner problem of SQL Server 2000 himslef, because this problem not occured anyway when I using MySQL; but when I invoke the sql generated by hibernate "select parent0_.PARENT_ID as PARENT1_0_0_ from PARENT_TABLE parent0_ where parent0_.PARENT_ID=?" directly from SQL Query Analyzer with parameter 1 as "1" or "1 "(significative when using CHAR column type), the result is right, 3 records!!

I am trying to integrate legacy system with EJB3.0 implementation, so this problem must be resolved, is there any body can help me, thank a lot!!


Top
 Profile  
 
 Post subject: Is there any body could give me a favor?
PostPosted: Mon Jan 16, 2006 8:45 pm 
Newbie

Joined: Wed Nov 02, 2005 10:49 pm
Posts: 12
I had post this topic for two weeks, but no body post any reply.
Could you do me a favor? because the answer is alos important for me (or any others) to integrate MS-SQL Server with Hibernate 3.1...


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.