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: Collections manipulation ?
PostPosted: Tue Apr 20, 2004 1:47 pm 
Newbie

Joined: Tue Apr 20, 2004 8:43 am
Posts: 6
All day I've been trying to understand how java collections are used in Hibernate
I realy need the help of the CLUB ;)


I use the foloving example

Code:
//Boss.java

/**
*
* @hibernate.class 
*/
public class Boss {
   private String branchName; 
   private Set employees =  new HashSet();
   private Long id;
   
/*  SOME CODE*/          


/**
* @hibernate.set lazy="false" inverse="true"
* @hibernate.collection-key column="boss"
* @hibernate.collection-one-to-many
*       class="h.Employee"

* @return Returns the employees.
*/
public Set getEmployees() {
   return employees;
}
/**
* @param employees The employees to set.
*/
public void setEmployee(Set employees) {
   this.employees = employees;
}   
}

=================================

// Employee.java

/**
*
* @hibernate.class 
*/
public class Employee  {
   
   private String name;
   private Long age;
   private Long id;
   private String post;
   private Boss boss;

/*  SOME CODE*/          

/**
* @hibernate.many-to-one
* @return Returns the boss.
*/
public Boss getBoss() {
   return boss;
}

/**
*
* @param boss The boss to set.
*/
public void setBoss(Boss boss) {
   this.boss = boss;
}   
}

=================================

//Main.java

/*  SOME CODE*/          

Iterator i = session.createQuery("select b from h.Boss b ").iterate();
         
while(i.hasNext()) {

   Boss boss = (Boss)i.next();
   Set eSet = boss.getEmployees();   
   Employee newE = new Employees();
   newE.setName("newE");   
   newE.setBoss(boss);
   session.save(newE);
   session.save(newE);      
   eSet.clear();      // ?   what happening      
   eSet.add(newE);                        // ?    what happening      
   session.save(newE);
   session.save(boss);                                             
   session.flush();

}

/*  SOME CODE*/          
=================================================

boss.getEmployees(); should return all Employees from Employee table whose boss the same . Right ?
but in my case it returns an emplty Set :(.

When i got a Set and i call differents set methods how these calls reflect on the persistent data ?

Thans a lot


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 20, 2004 2:09 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
setEmployee ??? broken get/set pair, it should be setEmployees


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 21, 2004 2:18 am 
Newbie

Joined: Tue Apr 20, 2004 8:43 am
Posts: 6
in the original code this pair is fine
setEmployees my misprint :(
but the problem exists


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 21, 2004 2:22 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
just a test:
Iterator i = session.createQuery("select b from h.Boss b left FETCH join b.employees").iterate();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 21, 2004 4:54 am 
Newbie

Joined: Tue Apr 20, 2004 8:43 am
Posts: 6
here is the result

Code:
/*   ...   */

   Boss boss = new Boss();
   Boss boss1 = new Boss();
   Boss boss2 = new Boss();
   Employee first = new Employee();
   Employee second = new Employee();
   Employee third = new Employee();
   
   
   
   
   first.setName("first");
   second.setName("second");
   third.setName("third");
   first.setBoss(boss1);                                                         
   second.setBoss(boss1);         
   third.setBoss(boss1);
   
   boss.setBranchName("boss");
   boss1.setBranchName("boss1");
   boss2.setBranchName("boss2");
   
   
   session..save(boss);
   session..save(boss1);
   session..save(boss2);
   session..save(first);
   session..save(second);
   session..save(third);         
            
   session..flush();
   Query q = session..createQuery("select b from h.Boss b left FETCH join b.employees");
   Iterator i = q.list().iterator();
   
   while(i.hasNext()) {
      
      Boss boss = (Boss)i.next();
      Set e = boss.getEmployees();
      System.out.println(""+boss.getBranchName()+" Employees : "+ e.size());

   }
   session..flush();   


/*   ...   */


OUTPUT

Code:
     [java] boss Employees : 0
     [java] boss1 Employees : 0
     [java] boss1 Employees : 0
     [java] boss1 Employees : 0
     [java] boss2 Employees : 0


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 21, 2004 5:00 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
hummm, trace sql generated and look at what is wrong , there might be something wrong in the mapping


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 21, 2004 5:19 am 
Newbie

Joined: Tue Apr 20, 2004 8:43 am
Posts: 6
generated ddl

Code:
drop table IF EXISTS Employee;
drop table IF EXISTS Boss;
create table Employee (
   id BIGINT NOT NULL AUTO_INCREMENT,
   post VARCHAR(255),
   boss BIGINT,
   age BIGINT,
   name VARCHAR(255),
   primary key (id)
);
create table Boss (
   id BIGINT NOT NULL AUTO_INCREMENT,
   branchName VARCHAR(255),
   primary key (id)
);
alter table Employee add index (boss), add constraint FK4AFD4ACE2E3B6D foreign key (boss) references Boss (id);


boss.hbm.xml

Code:
   <?xml version="1.0"?>
   
   <!DOCTYPE hibernate-mapping PUBLIC
       "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
       "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
   
   <hibernate-mapping>
       <class
           name="h.Boss"
           dynamic-update="false"
           dynamic-insert="false"
       >
   
           <id
               name="id"
               column="id"
               type="java.lang.Long"
               unsaved-value="null"
           >
               <generator class="native">
               </generator>
           </id>
   
           <property
               name="branchName"
               type="java.lang.String"
               update="true"
               insert="true"
               column="branchName"
           />
   
           <set
               name="employees"
               lazy="false"
               inverse="true"
               cascade="none"
               sort="unsorted"
           >
   
                 <key
                     column="boss"
                 />
   
                 <one-to-many
                     class="h.Employee"
                 />
           </set>
   
           <!--
               To add non XDoclet property mappings, create a file named
                   hibernate-properties-Boss.xml
               containing the additional properties and place it in your merge dir.
           -->
   
       </class>
   
   </hibernate-mapping>



Employee.hbm.xml

Code:
<?xml version="1.0"?>

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

<hibernate-mapping>
    <class
        name="h.Employee"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="id"
            type="java.lang.Long"
            unsaved-value="null"
        >
            <generator class="native">
            </generator>
        </id>

        <property
            name="post"
            type="java.lang.String"
            update="true"
            insert="true"
            column="post"
        />

        <many-to-one
            name="boss"
            class="h.Boss"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="boss"
        />

        <property
            name="age"
            type="java.lang.Long"
            update="true"
            insert="true"
            column="age"
        />

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            column="name"
        />

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

    </class>

</hibernate-mapping>



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.