-->
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.  [ 1 post ] 
Author Message
 Post subject: Aide Avec Mapping en Annotations
PostPosted: Thu Aug 27, 2009 1:27 pm 
Newbie

Joined: Thu Aug 27, 2009 11:59 am
Posts: 4
Salut!
Je suis début en hibernate mapping, et j'ai besion de faire le "mapping" pour quatre classes.
Les tables sont:

Code:
CREATE TABLE Parent(
  Parent_Id NUMERIC(5, 0)  NOT NULL ,
  Descript VARCHAR(60)  NOT NULL,
  PRIMARY KEY(Parent_Id)
);

CREATE TABLE State (
  Parent_Id NUMERIC(5, 0)  NOT NULL,
  State_Id VARCHAR(2)  NOT NULL,
  Descript VARCHAR(60)  NOT NULL,
PRIMARY KEY(Parent_Id, State_Id),
INDEX IDX_S(Parent_Id),
  FOREIGN KEY(Parent_Id)
      REFERENCES Parent(Parent_Id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION);

CREATE TABLE Area (
  Parent_Id NUMERIC(5, 0)  NOT NULL,
  State_Id VARCHAR(2)  NOT NULL,
  Area_Code NUMERIC(5,0)  NOT NULL,
  Descript VARCHAR(85)  NULL,
PRIMARY KEY(Parent_Id, State_Id, Area_Code) ,
INDEX IDX_A(Parent_Id, State_Id),
  FOREIGN KEY(Parent_Id, State_Id)
    REFERENCES State(Parent_Id, State_Id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION);

CREATE TABLE Result(
  Parent_Id   NUMERIC(5, 0) NOT NULL,
  State_Id    VARCHAR(2)    NOT NULL,
  Result_Code NUMERIC(6, 0) NOT NULL,
  Area_Code   NUMERIC(5, 0) NOT NULL,
  Result_Text VARCHAR(80)   NOT NULL,
  PRIMARY KEY(Main_Id, State_Id, Result_Code),
  INDEX IDX_State(Parent_Id, State_Id),
  FOREIGN KEY       (Parent_Id, State_Id)
    REFERENCES State(Parent_Id, State_Id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION,
  INDEX IDX_Area(Parent_Id, State_Id, Area_Code),
  FOREIGN KEY      (Parent_Id, State_Id, Area_Code)
    REFERENCES Area(Parent_Id, State_Id, Area_Code)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION
);



Je voudrais de faire quatre classes POJO pour chaqu'une.
J'ai fait ça pour les trois prémière, et ça marche bien:

Code:
package dbo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="Parent")
public class Parent{
  @Id
  @Column(name="Parent_Id", precision=5, scale=0)
  private int parentId;
 
  @Column(name="Descript", length=60)
  private String descript;

  public int getParentId(){
    return parentId;
  }

  public void getParentId(int parentId){
    this.parentId= parentId;
  }

  public String getDescript(){
    return descript;
  }

  public void setDescript(String descript){
    this.descript= descript;
  }
 
}


Code:
package dbo;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="State")
public class State implements Serializable{
  private static final long serialVersionUID= 6030875192113837000L;

  @Id
  private PKState id;
 
  @Column(name="Descript", length=60)
  private String descript;

  public PKState getId(){
    return id;
  }

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

  public String getDescript(){
    return descript;
  }

  public void setDescript(String descript){
    this.descript= descript;
  }

  public boolean equals(Object state){
    return state!= null && state instanceof State &&
           ((State)state).getId().equals(id);
  }
 
  public int hashCode(){
    return id.hashCode();
  }
 
}


Code:
package dbo;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Embeddable
public class PKState implements Serializable{
  private static final long serialVersionUID= 4994565448557556944L;
 
  @ManyToOne
  @JoinColumn(name="Parent_Id")
  private Parent parent;
 
  @Column(name="State_Id")
  private String stateId;
 
  public PKState(){
    parent = new Parent();
  }
 
  public Parent getParent(){
    return parent;
  }

  public void setParent(Parent parent){
    this.parent= parent;
  }
 
  public String getStateId(){
    return stateId;
  }

  public void setStateId(String stateId){
    this.stateId= stateId;
  }
 
  public boolean equals(Object id){
    return id != null && id instanceof PKState &&
           ((PKState)id).getParent().getParentId() == parent.getParentId() &&
           ((PKState)id).getStateId().equals(stateId);
  }
 
  public int hashCode(){
    return 3*parent.getParentId();
  }

}


Code:
package dbo;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="Area")
public class Area implements Serializable{
  private static final long serialVersionUID= -1465717945781167218L;
 
  @Id
  private PKArea id;
 
  @Column(name="Descript", length=85)
  private String descript;

  public PKArea getId(){
    return id;
  }

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

  public String getDescript(){
    return descript;
  }

  public void setDescript(String descript){
    this.descript= descript;
  }
 
  public boolean equals(Object area){
    return area != null && area instanceof Area &&
           ((Area)area).getId().equals(id);
  }
 
  public int hashCode(){
    return id.hashCode();
  }

}


Code:
package dbo;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;

@Embeddable
public class PKArea implements Serializable{
  private static final long serialVersionUID= -137835683278214689L;
 
  @ManyToOne
  @JoinColumns({
    @JoinColumn(name="Parent_Id"),
    @JoinColumn(name="State_Id")
  })
  private State state;
 
  @Column(name="Area_Code", precision=5, scale=0)
  private int areaCode;
 
  public PKArea(){
    state= new State();
  }

  public State getState(){
    return state;
  }

  public void setEstado(State state){
    this.state= state;
  }

  public int getAreaCode(){
    return areaCode;
  }

  public void setAreaCode(int areaCode){
    this.areaCode= areaCode;
  }
 
  public boolean equals(Object id){
    return id != null && id instanceof PKArea &&
           ((PKArea )id).getEstado().equals(state) &&
           ((PKArea )id).getAreaCode() == areaCode;
  }
 
  public int hashCode(){
    return 7*areaCode;
  }

}


Pardon pour le français et pour la long discussion. :)
Ma demande est: comment je peux faire le "mapping" pour le table "Result" et avoir un objet pour le table "Area"?
Je voudrais pouvoir faire quelque chose comme ça:

Code:
Result result = ....;
result.getArea().getId()...;
result.getParent()....;


Quand j'ai essayé faire ça, j'ai reçu une message en parlant que je ne peux pas utiliser une
colonne deux fois...
Merci!

Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.