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: Help mapping this two objects
PostPosted: Wed Jul 09, 2008 11:28 am 
Newbie

Joined: Wed Jul 09, 2008 11:17 am
Posts: 2
I'm trying to learn hibernate and I'm stuck mapping this two object:

Class CD:


Code:
public class CD {
   
   private int idCD;
   private String title;
   private List tracks = new ArrayList();
   
   public CD() {
      
        }
//getter and setter methods


and class Track:
Code:
public class Track {
   private int idTrack;
   private String title;
   
   public Track() {
      
   }
//getter and setter methods


The sql of the table for the CD objects:
Code:
CREATE TABLE tb_cd
(
  title character varying(255),
  id_cd serial NOT NULL,
  CONSTRAINT pk_id_cd PRIMARY KEY (id_cd)
)

...and for the Track objects:

Code:
CREATE TABLE tb_track
(
  id_track serial NOT NULL,
  title character varying(255),
  cd_id integer NOT NULL,
  "order" integer NOT NULL,
  "position" integer,
  CONSTRAINT pk_id_track PRIMARY KEY (id_track),
  CONSTRAINT fk_id_cd FOREIGN KEY (cd_id)
      REFERENCES tb_cd (id_cd) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT fkfadf583a1a2704c7 FOREIGN KEY (cd_id)
      REFERENCES tb_cd (id_cd) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)


the relation between CD and Track are 1-->N and unidirectional. What are the correct mapping for this two class?

(The column "position" is for the <list-index> )


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 09, 2008 5:09 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Here's a little tutorial on performing bi-directional one-to-many relationships:

http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=18mappingonetomanyassociations

One thing I might suggest is to do the actual Java coding first, and then have the SchemaExport class create the underlying database tables. That way, the database will elute naturally from your object model. Or are you locking into using that particular database?

_________________
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 Jul 10, 2008 3:14 am 
Newbie

Joined: Wed Jul 09, 2008 11:17 am
Posts: 2
Cameron McKenzie wrote:
Or are you locking into using that particular database?


Thanks a lot for the tips but I had to use this schema.

EDIT: I made it!
Here the two config files:
CD.hbm.xml
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" >

<hibernate-mapping>
   <class name="it.dpp.cd.entities.CD" table="tb_cd">
      <id name="idCD" column="id_cd">
         <generator class="sequence" />
      </id>
      <property name="title" column="title" />
      <list name="tracks" cascade="all">
         <key column="cd_id" not-null="true" />
         <list-index column="position" />
         <one-to-many class="it.dpp.cd.entities.Track" />
      </list>
   </class>
</hibernate-mapping>


and Track.hbm.xml:
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" >

<hibernate-mapping>
   <class name="it.dpp.cd.entities.Track" table="tb_track">
      <id name="idTrack" column="id_track">
         <generator class="sequence" />
      </id>
      <property name="title" column="title" />
   </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.  [ 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.