-->
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.  [ 13 posts ] 
Author Message
 Post subject: One-To-Many Customize
PostPosted: Thu Aug 02, 2007 5:35 am 
Newbie

Joined: Thu Aug 02, 2007 5:23 am
Posts: 11
HBM:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 1-ago-2007 16.55.03 by Hibernate Tools 3.2.0.b9 -->
<hibernate-mapping>
<class name="bean.Table1" table="TABLE1" schema="dbo" catalog="test">
<id name="id" type="long">
<column name="ID" />
<generator class="assigned" />
</id>

<property name="typeRow" type="string">
<column name="typeRow" length="2" />
</property>

<set name="table2s" lazy="false">
<key>
<column name="id" not-null="true" unique="true" />
</key>
<one-to-many class="bean.Table2" />
</set>
<set name="table3s" lazy="false">
<key>
<column name="id" not-null="true" unique="true" />
</key>
<one-to-many class="bean.Table3" />
</set>

</class>
</hibernate-mapping>

JavaCode:

Table1 table1 = (Table1)getHibernateTemplate().get(Table1.class, id);

The problem:
Hibernate will make retrieve for every Table 2 and Table 3.

I want that he make retrieve ONLY:
1. if Table1.typeRow = 2 then Table2.retrieve
2. if Table2.typeRow = 3 then Table3.retrieve
3. if Table3.typeRoe = null then do nothing


Is it possible to make configuration in HBM file, or I must write all SQL code for this.

Thank you very much on respones.

p.s. Sorry for my english


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 03, 2007 3:08 am 
Newbie

Joined: Thu Aug 02, 2007 5:23 am
Posts: 11
Can I use Descriminator for solution of my problem? If I can, please can someone tell me more informations about that.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 03, 2007 6:35 pm 
Newbie

Joined: Thu Aug 02, 2007 5:23 am
Posts: 11
please any information!!!!

If someone don't understand me please say I can put all my code!!!


thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 05, 2007 1:45 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Are you saying Table1 objects always have Table2s and Table3s but you want to control eager fetching based on the type?

Or does a Table1 object with type=2 never have Table3 objects, with type=3 never have Table2 objects and type=3 have neither?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 05, 2007 5:15 pm 
Newbie

Joined: Thu Aug 02, 2007 5:23 am
Posts: 11
Answer is this:

Table1 object with type=2 never have Table3 objects, with type=3 never have Table2 objects and type=3 have neither.

Example:

Table1
1 row type=2
2 row type=3

Table 2
1 row somedata22
2 row somedata222

Table 3
1 row somedata33
2 row somedata333

pseudo: table1.retrieve()
SQL:
1. SELECT * FROM table1;
In first bean, in property type, I have 2, and he must "SEE or UNDERSTAND" that he must do:
SELECT * FROM table2; but not
SELECT * FROM table3;

In second bean, in property type, I have 3, and he must "SEE or UNDERSTAND" that he must do:
SELECT * FROM table3; but not
SELECT * FROM table2;





Now it function like this:

pseuod: table1.retrieve()
SQL:
1. SELECT * FROM table1;
And for every row he do:
SELECT * FROM table2;
SELECT * FROM table3;


It's not problem if I have just 5-10 rows, but You can immagine how it's slow when I have 500 000-1 000 000 rows.

PS. I can change tables.


Thank you very, very, very much


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 05, 2007 5:23 pm 
Newbie

Joined: Thu Aug 02, 2007 5:23 am
Posts: 11
solution:

class table1{
private int type = 0
private Table2 table2;
private Table3 table3;

public void setType(int type){
//controll retrieve
if type==2 table2.retrieve();
if type==3 table3.retrieve();

this.type = type
}

public void getTyoe(){
return type
}

}

in HBM configuration file you must move join with table 2 and table 3 (ovveride hibernate retrieve).



This solution work, but problem is that I ovveride Hibernate, and I don't want do that. I want that everything is in Configuration file.


Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 05, 2007 5:50 pm 
Newbie

Joined: Thu Aug 02, 2007 5:23 am
Posts: 11
I wrote that I can change tables......This isn't true.....I can't change tables:)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 05, 2007 6:34 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
How about this... create a separate java class for each of your table types and use "table per class hierarchy" to persist them. Type 2 would have a collection of Table2Data objects, type 3 would have a collection of Table3Data objects, type 0 would have no collections.

e.g.
Table.java
Code:
package test.tableperhierarchy;
public abstract class Table {
   private Long id;
   public Long getId() {
      return id;
   }
   public void setId(Long id) {
      this.id = id;
   }
}



TableWithNoTypeData.java
Code:
package test.tableperhierarchy;
public class TableWithNoTypeData extends Table {
}


TableWithType2Data.java
Code:
package test.tableperhierarchy;

import java.util.Set;

public class TableWithType2Data extends Table {

   private Set<Type2Data> type2Data;

   public Set<Type2Data> getType2Data() {
      return type2Data;
   }

   public void setType2Data(Set<Type2Data> type2Data) {
      this.type2Data = type2Data;
   }
}


TableWithType3Data.java
Code:
package test.tableperhierarchy;

import java.util.Set;

public class TableWithType3Data extends Table {

   private Set<Type3Data> type3Data;

   public Set<Type3Data> getType3Data() {
      return type3Data;
   }

   public void setType3Data(Set<Type3Data> type3Data) {
      this.type3Data = type3Data;
   }
}


Table.hbm.xml
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="test.tableperhierarchy">

<class name="Table" table="tableperhierarchy" discriminator-value="0">
   <id name="id">
      <generator class="native"/>
   </id>
   
   <discriminator column="table_type" type="integer"/>
   
   <subclass name="TableWithType2Data" discriminator-value="2">
      <set name="type2Data">
         <key column="table_id"/>
         <one-to-many class="Type2Data"/>
      </set>
    </subclass>

   <subclass name="TableWithType3Data" discriminator-value="3">
      <set name="type3Data">
         <key column="table_id"/>
         <one-to-many class="Type3Data"/>
      </set>
    </subclass>

   <subclass name="TableWithNoTypeData" discriminator-value="0">
    </subclass>
   
</class>
</hibernate-mapping>


Type2Data.java
Code:
package test.tableperhierarchy;

public class Type2Data {

   private Long id;

   public Long getId() {
      return id;
   }

   public void setId(Long id) {
      this.id = id;
   }
}


Type2Data.hbm.xml
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="test.tableperhierarchy">

<class name="Type2Data" table="type2data">
   <id name="id">
      <generator class="native"/>
   </id>
</class>
</hibernate-mapping>


Type3Data.java
Code:
package test.tableperhierarchy;

public class Type3Data {

   private Long id;

   public Long getId() {
      return id;
   }

   public void setId(Long id) {
      this.id = id;
   }
}


Type3Data.hbm.xml
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="test.tableperhierarchy">

<class name="Type3Data" table="type3data">
   <id name="id">
      <generator class="native"/>
   </id>
</class>
</hibernate-mapping>



TestIt.java
Code:
package test.tableperhierarchy;

import java.util.HashSet;
import java.util.Set;

import junit.framework.TestCase;

import org.hibernate.Session;

public class TestIt extends TestCase {

   public void testIt() {
      createObjects();
      
      Session s = HibernateUtil.getSession();
      s.beginTransaction();
      
      TableWithType2Data tableWithType2Data = (TableWithType2Data)s.load(TableWithType2Data.class, new Long(2));
      assertEquals(3, tableWithType2Data.getType2Data().size());
      
      s.getTransaction().commit();
      s.close();
   }

   private void createObjects() {
      Session s = HibernateUtil.getSession();
      s.beginTransaction();
      
      TableWithNoTypeData table1 = new TableWithNoTypeData();
      s.save(table1);
      
      Type2Data type2Data1 = new Type2Data();
      s.save(type2Data1);
      Type2Data type2Data2 = new Type2Data();
      s.save(type2Data2);
      Type2Data type2Data3 = new Type2Data();
      s.save(type2Data3);
      
      Set<Type2Data> type2DataSet = new HashSet<Type2Data>();
      type2DataSet.add(type2Data1);
      type2DataSet.add(type2Data2);
      type2DataSet.add(type2Data3);
      TableWithType2Data table2 = new TableWithType2Data();
      table2.setType2Data(type2DataSet);
      s.save(table2);
      
      Type3Data type3Data1 = new Type3Data();
      s.save(type3Data1);
      Type3Data type3Data2 = new Type3Data();
      s.save(type3Data2);
      Type3Data type3Data3 = new Type3Data();
      s.save(type3Data3);
      
      Set<Type3Data> type3DataSet = new HashSet<Type3Data>();
      type3DataSet.add(type3Data1);
      type3DataSet.add(type3Data2);
      type3DataSet.add(type3Data3);
      TableWithType3Data table3 = new TableWithType3Data();
      table3.setType3Data(type3DataSet);
      s.save(table3);
      
      s.getTransaction().commit();
      s.close();
   }
}


HibernateUtil.java
Code:
package test.tableperhierarchy;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    private static SessionFactory factory;

    static {
       Configuration config = new Configuration()
          .addClass(Table.class)
          .addClass(Type2Data.class)
          .addClass(Type3Data.class)

// MYSQL config          
          .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect")
         .setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
         .setProperty("hibernate.connection.url", "jdbc:mysql://localhost/test")
         .setProperty("hibernate.connection.username", "root")
         .setProperty("hibernate.connection.password", "password")
         .setProperty("hibernate.hbm2ddl.auto", "create-drop")
                  
         .setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider")
         .setProperty("hibernate.show_sql", "true");



        HibernateUtil.setSessionFactory(config.buildSessionFactory());
    }

    public static synchronized Session getSession() {
        if (factory == null) {
            factory = new Configuration().configure().buildSessionFactory();
        }
        return factory.openSession();
    }

    public static void setSessionFactory(SessionFactory factory) {
        HibernateUtil.factory = factory;
    }
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 08, 2007 3:06 am 
Newbie

Joined: Thu Aug 02, 2007 5:23 am
Posts: 11
With discriminator work's. Thank you vary much. But I have new question.

<subclass name="TableWithType3Data" discriminator-value="3">
<set name="type3Data">
<key column="table_id"/>
<one-to-many class="Type3Data"/>
</set>
</subclass>


subclass name why it must be new Class, different of one-to-many class. Is it possible to use just one class.

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 08, 2007 7:43 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
You can't have 1 class and use the discriminator column. Think about it... if it were one class, when hibernate came to persist a new entry how would it choose a discriminator value? It couldn't.

You can give the appearance of just one class by using the base class. I'm not sure why you would do this though - perhaps if you describe why this is important to you it would help.

In the base class add methods to get the type2 and type3 data that return null.

Table.java
Code:
   public Set<Type2Data> getType2Data() {
      return null;
   }
   public Set<Type3Data> getType3Data() {
      return null;
   }


The appropriate method is then overridden in the subclasses. When you query or load you can just reference the base class:

Code:
Table someTable = (Table)s.load(Table.class, new Long(2));


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 08, 2007 8:05 am 
Newbie

Joined: Thu Aug 02, 2007 5:23 am
Posts: 11
I will now give you all code.

Table.java
Code:
package bean;

import java.util.HashSet;
import java.util.Set;

public class Table implements java.io.Serializable {

   private long idTable;
   private String tableType;
   private Set table2 = new HashSet(0);
   private Set table3 = new HashSet(0);

   public Table() {
   }

   .
   .
   .
   
   public void setTable2(Set table2)
   {
      this.table2 = table2
   }
   
   public void setTable3(Set table3)
   {
      this.table3 = table3
   }

}


TableEmpty.java
Code:
package bean;
import bean.Table;

public class TableEmpty extends Table implements java.io.Serializable {

   }


Table2.java
Code:
package bean;
import bean.Table;

public class Table2
{   private long id;
   private String test;
   
   public void setId(long id)
   {
      this.id = id
   }
   
   public void setTest(String test)
   {
   this.test = test
   }
   
   .
   .
   .

   }



Table.hbm


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 3-ago-2007 9.04.09 by Hibernate Tools 3.2.0.b9 -->
<hibernate-mapping>
    <class name="bean.Table" table="TABLE" schema="dbo" catalog="Test" >
        <id name="idTable" type="long">
            <column name="ID_TABLE" />
            <generator class="assigned" />
        </id>       
        <discriminator column="TABLE_TYPE" type="string"/>
     
    <subclass name="bean.TableEmpty" extends="bean.Righe" discriminator-value="2">
      <set name="table2">
         <key column="idTable"/>
         <one-to-many class="bean.Table2"/>
      </set>
    </subclass>
   
    <subclass name="bean.TableEmpty" discriminator-value="3">
      <set name="table3">
         <key column="idTable"/>
         <one-to-many class="bean.Table3"/>
      </set>
    </subclass>     
    </class>
</hibernate-mapping>



If I understand it good.....TableEmpty MUST extends bean.Table. What you think about this code. This is perfect for me, because I can change tables.



One other thing....do you maybe know perfomance struts versus jsf....some documentation....thank you vary much


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 08, 2007 8:34 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
No, this is wrong. Each subclass has to be a separate class - you can't use TableEmpty for both. Yes, they must all extend the base class.

The "extends" attribute is for defining a subclass in a separate mapping file. You can't have a subclass inside a class mapping that extends a different bean type (bean.Righe).

Quote:
This is perfect for me, because I can change tables.

What do you mean? Your java classes should model the problem domain and not care about the tables.

Quote:
do you maybe know perfomance struts versus jsf

No but I doubt there's a great deal of difference. Any performance problems you'll have in your application are much more likely to be data access related. You should choose the framework that best suits your needs and/or the capabilities of your team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 09, 2007 3:09 am 
Newbie

Joined: Thu Aug 02, 2007 5:23 am
Posts: 11
You have right, I can't use same class two times.

But now I have new BIG problem.

I will open new topic "Mapping two tables from two database", thank you very much.


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