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.  [ 3 posts ] 
Author Message
 Post subject: hibernate mutiple table to on java class
PostPosted: Tue Jun 24, 2008 1:55 pm 
Hi,

Could someone have a look on this.
I have only one java class called InfoData. This class gets its information from different tables.

Could you please explain me how <many-to-many> and <many-to-one> works
when I have more than one table in the mapping.

I have also find the example in the reference (Chapter 5.3 Basic O/R Mapping).
I tried to solve my problem by using the enity-name attribute like it is shown in this sample . But it did not work.
How does this work when I have not a Set in my class ? Please help.

I tried to create the xml mapping. Please have a look on it (see above).
Thanks in advance


Code:
public class InfoData  {

   private Integer reportID;               
   private String reportName;            
         
   private DBUser user;                     
   private Engine engine;                  
   private Set exportFormatSet;
   
}


xml mapping
Code:
<class name="InfoData" table="REPORT"
      entity-name="currentReport" />
   <id name="reportID" type="integer" column="REPORT_ID" />
   <property name="reportName" type="string" column="REPORT_NAME" />
   
   <many-to-one name="engine" entity-name="currentEngine">
      <column name="engineId" />
   </many-to-one>
   
   <many-to-one name="user" entity-name="currentDbUser">
      <column name="dbuserId" />
   </many-to-one>
   
   <set name="exportFormatSet" table="REPORT_EXPORT_MAP" lazy="false"
      sort="natural">
      <key column="reportID" />
      <many-to-many column="EXPORT_ID" fetch="join"
         class="ExportFormat" />
   </set>
   
</class>

<class name="InfoData" table="ENGINE"
      entity-name="currentEngine" >
      <many-to-one name="engine"
      column="engineId" entity-name="currentReport"/>
</class>

<class name="InfoData" table="DB_USER"
      entity-name="currentDbUser" >
      <many-to-one name="user"
         column="dbuserId" entity-name="currentReport"/>
</class>


Top
  
 
 Post subject:
PostPosted: Wed Jun 25, 2008 10:00 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
So, I'm going to break your question down into it's first sample part. Then we can work from there.

You said your class gets it's data from multiple tables, right? To do such a mapping, where one class goes to two tables, you use the SecondaryTable annotation.

So, take a look at this scenario, where one class maps to two database tables:


http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=13mappingoneclasstotwodatabasetablesjpahibernate

Image

The class ends up looking like this, where the single class has annotations for the primary Entity table, and the Secondary table as well.


Code:
package com.examscam.mappings;
import javax.persistence.*;import org.hibernate.Session;
import com.examscam.HibernateUtil;

@Entity
@Table(name="bar")
@SecondaryTable(name="foo")
public class FooBar {
  int id;
  String fooName;
  String barCode;

  @Id
  @GeneratedValue
  public int getId() {return id;}
  public void setId(int id) {this.id = id;}

  @Column(table="foo")
  public String getFooName() {return fooName;}
  public void setFooName(String fooName) {
    this.fooName = fooName;
  }

  /* no need for mapping-goes to default bar table */
  public String getBarCode() {return barCode;}
  public void setBarCode(String barCode) {
    this.barCode = barCode;
  }

  public static void main(String args[]) {
/*HibernateUtil needs FooBar.class in AnnotationConfiguration*/
    HibernateUtil.recreateDatabase();
    FooBar fb = new FooBar();
    fb.setBarCode("90210");
    fb.setFooName("ManChu");
     Session session = HibernateUtil.beginTransaction();
    session.save(fb);
    HibernateUtil.commitTransaction();
  }
}


So, that tackles the first part of using one class and mapping it to two tables.

Now, what's next? If you can get over that hurdle, what is the next hurdle to jump over?

One step at a time, and we'll get there!

The site I linked to also has tutorials on one-to-many and many-to-many mapping scenarios as well. Check them out and post back!

http://jpa.thebookonhibernate.com/Javacode/learn.jsp?tutorial=19mappingmanytomanyrelationships

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 26, 2008 5:16 am 
Thanks a lot for your help.

But my scenario looks a little bit different to yours.
I think it is the best way when I give you further information.

The database schema looks like this:

Code:
CREATE TABLE foo (
    foo_id    int unsigned( 10 ) NOT NULL,
    name    VARCHAR( 45 ) NOT NULL,
    CONSTRAINT pk_foo PRIMARY KEY(foo_id));

CREATE TABLE info (
    info_id    int unsigned( 10 ) NOT NULL,
    information    VARCHAR( 45 ) NOT NULL,
    CONSTRAINT pk_info PRIMARY KEY(info_id)) ;

CREATE TABLE bar (
    bar_id    int unsigned( 10 ) NOT NULL,
    name    VARCHAR( 45 ) NOT NULL,
    info_id    int unsigned( 10 ) NOT NULL,
    CONSTRAINT pk_bar PRIMARY KEY(bar_id),
    CONSTRAINT FK_bar_1 FOREIGN KEY( info_id ) REFERENCES info ( info_id )) ;

  CREATE TABLE foo_bar_map (
    id    int unsigned( 10 ) NOT NULL,
    foo_id    int unsigned( 10 ) NOT NULL,
    bar_id    int unsigned( 10 ) NOT NULL,
    CONSTRAINT pk_foo_bar_map PRIMARY KEY(id),
    CONSTRAINT FK_FOO_BAR_MAP_1 FOREIGN KEY( bar_id ) REFERENCES bar ( bar_id ),
    CONSTRAINT FK_FOO_BAR_MAP_2 FOREIGN KEY( foo_id ) REFERENCES foo ( foo_id ));


I created following classes:

Code:
public class Foo {
   private Integer fooId;
   private String name;
   
   ...
}

public class Bar {
   private Integer barId;
   private String name;
       private Info info;
   ...
}

public class FooBarMap {
   private Integer id
   private Foo foo;
   private Bar bar;
...
}

public class Info {
   private Integer id
   private String info
}


Now I created a class where I collect informations about these
classes.

Code:
public class Infodata {

private Integer barId;
private String infoName;
private Set foos;

...
}


Please have a look on it again. How can I create the hibernate xml mapping for the "InfoData" bean?

Sorry, I have forgotten to mention that I have to use Java 1.4 .
So I cannot use annotations :-(. But I think there is no problem to migrate the annotations to the xml mapping.


Thanks in advance.


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