-->
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: Composite Key Confusion
PostPosted: Fri Oct 19, 2007 1:19 pm 
Newbie

Joined: Thu Mar 15, 2007 1:22 am
Posts: 11
A bit confused about mapping a table.
I want the table to look like this:
table JobFile
column waittime
column transfertime
column transferSpeed
column Job references table Job
column File (String representing full path to file)
primary key(job,file)

----------------------
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 package="com.aol.hermes.common">
  <class name="JobFile" table="jobfile">
     <composite-id name="jobFileID" class="JobFile$ID">
      <key-property name="job" column="job" type="com.aol.hermes.common.Job" />
        <key-property name="file" column="file" type="java.lang.String"/>
   </composite-id>
       
     <property name="waitTime" type="java.lang.Integer" column="waittime" not-null="true"/>
     <property name="transferTime" type="java.lang.Integer" column="transfertime" not-null="true"/>
     <property name="transferSpeed" type="java.lang.Integer" column="transferkbps" not-null="true"/>

  </class>
</hibernate-mapping>



Any advice would be helpful. I am confused why there is a need for a class to represent a composite primary key, but, whatever works will work :)

Thomas


Top
 Profile  
 
 Post subject: Hi
PostPosted: Fri Oct 19, 2007 3:58 pm 
Newbie

Joined: Tue Jun 12, 2007 6:44 am
Posts: 13
Location: Brazil
Using xml with hibernate sucks!!! Why dont you try annotations.

If you have a N:N relationship between two tables, you have to create another table (associative table). The same applies to Persistent classes (annotated or via XML), you have to create an associative persistent class.

A composite key is an attribute compound of more than one value, this means that the pair of values that compose the PK must be unique in the table, right?

Thats why you have to create a class to represents the composite primary key. Then you have to code the equals and hashcode methods.

*----------------------------------------------------------------------------------*
This example is composed of two entities(persistent classes):
-Curso
-Disciplina

With the following Relationship:
Curso * * Disciplina

Of course another table is required, I concatenated the names of the involved tables:
CursoDisciplina

But there is a restriction:
One course cant be related to the same disciplina more than one time (the same applies to disciplina).

The solution:
Use the entities attributes as a compound PK in the associative table

This is an example of a composite PK class:
@Embeddable
public class ChaveComposta implements java.io.Serializable {

@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="id_curso")
private Curso curso;


@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="id_disciplina")
private Disciplina disciplina;


/** Creates a new instance of ChaveComposta */
public ChaveComposta() {
}

public Curso getCurso() {
return curso;
}

public void setCurso(Curso curso) {
this.curso = curso;
}

public Disciplina getDisciplina() {
return disciplina;
}

public void setDisciplina(Disciplina disciplina) {
this.disciplina = disciplina;
}

}


and here an associative entity(class) that establishes a relation between two other entities(classes).@Entity
@Table(name="curso_disciplina")
public class CursoDisciplina implements java.io.Serializable {

@EmbeddedId
private ChaveComposta chaveComposta;

@Column(name="dataAssociacao")
@Temporal(TemporalType.DATE)
private Calendar dataAssociacao;

/** Creates a new instance of CursoDisciplina */
public CursoDisciplina() {
}

public ChaveComposta getChaveComposta() {
return chaveComposta;
}

public void setChaveComposta(ChaveComposta chaveComposta) {
this.chaveComposta = chaveComposta;
}

public Calendar getDataAssociacao() {
return dataAssociacao;
}

public void setDataAssociacao(Calendar dataAssociacao) {
this.dataAssociacao = dataAssociacao;
}

}



This is just a snippet I have the project (Netbeans 5.5) here.
Email me: charlles.cuba@gmail.com


Top
 Profile  
 
 Post subject: Dont get confused by the above
PostPosted: Fri Oct 19, 2007 8:10 pm 
Newbie

Joined: Mon Aug 13, 2007 11:54 am
Posts: 6
composite classes are simple once you get use to them. First your mapping seems to be correct you need 2 class one for the for the table and one for the composite key.

you have Jobfile class
Code:
class Jobfile{

    JobFileID jobFileID = new JobFileID();

    <--- other column's but not the composite column's

    public void setJobFileID(JobFileID jobFileID){
         this.jobFileID= jobFileID;
    }

    public JobFileID getJobFileID(){
         return jobFileID;
    }

}


then you have the class for the composite key

Code:
class JobFileID {

    String job;
    String file;

    public void setJob(String job){
         this.job= job;
    }

    public String getJob(){
         return job;
    }

    public void setFile(String file){
         this.file= file;
    }

    public String getfile(){
         return file;
    }

}


I dont know the exact reason that hibernate likes this but thats how it wants it. Once you start doing it it makes for better code when dealing with the objects and there associations. Once you get the JobFile Object you can make calls like this

Code:
JobFile.getJobFileID.getJob();


replace the String in JobFileId if you are calling to a specific objects so that the above code would return the Job Object joined to the JobFileId.


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