-->
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.  [ 5 posts ] 
Author Message
 Post subject: Help with Mapping In Annotations
PostPosted: Thu Aug 27, 2009 1:15 pm 
Newbie

Joined: Thu Aug 27, 2009 11:59 am
Posts: 4
Hi everyone!
I'm beginner at hibernate mapping, and I need to map Four classes.
The tables are:

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
);



I'd like to create four POJO classes for each one of them.
I did that to the first three, and worked pretty good:

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;
  }

}


Sorry by the long code, but I tried to show everything.
The question is: how can I crate a map for the table Result and use an object reference for table "Area" by using the field Area_Code?
I'd like to can do something like that:

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


When I tried to map the class, I received an error saying I cannot use a column twice...
Anything can help me?

Thanks!


Top
 Profile  
 
 Post subject: Re: Help with Mapping In Annotations
PostPosted: Thu Aug 27, 2009 3:13 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Does it tell you which column?

_________________
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: Re: Help with Mapping In Annotations
PostPosted: Thu Aug 27, 2009 4:02 pm 
Newbie

Joined: Thu Aug 27, 2009 11:59 am
Posts: 4
I can map the column "Area_Code", from the table "Result", like a primitive field; that's work fine.
But I want to map a field like an object field. To be more specifc, I mean an object that map the table "Area" from the table "Result".


Top
 Profile  
 
 Post subject: Re: Help with Mapping In Annotations
PostPosted: Thu Aug 27, 2009 6:19 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
I was thinking more along the lines of which column does it say you are mapping twice?

_________________
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: Re: Help with Mapping In Annotations
PostPosted: Fri Aug 28, 2009 6:36 am 
Newbie

Joined: Thu Aug 27, 2009 11:59 am
Posts: 4
Sorry!
The message error appears if I map the table "Result" with something like that:

Code:
package dbo;

import java.io.Serializable;

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

@Entity
@Table(name="Result")
public class Result implements Serializable{
  private static final long serialVersionUID= 6225912140774410754L;
 
  @Id
  private PKResult id;
 
@OneToOne
  @JoinColumns({
    @JoinColumn(name="Parent_Id"),
    @JoinColumn(name="State_Id"),
    @JoinColumn(name="Area_Code")
  })
  private Area area; 
 
  @Column(name="Descript", length=80)
  private String descript;

  public PKResult getId(){
    return id;
  }

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

  public Area getArea(){
    return area;
  }

  public void setArea(Area area){
    this.area= area;
  }

  public String getDescript(){
    return descript;
  }

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

}


And the PKResult:

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 PKResult implements Serializable{
  private static final long serialVersionUID= -7540842434151968481L;
 
  @ManyToOne
  @JoinColumns({
    @JoinColumn(name="Parent_Id"),
    @JoinColumn(name="State_Id")
  })
  private Estado estado;
 
  @Column(name="Result_Code", precision=6, scale=0)
  private int resultCode;

  public PKResult(){
    state = new State();
  }

  public State getState(){
    return state;
  }

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

  public int getResultCode(){
    return resultCode;
  }

  public void setResultCode(int resultCode){
    this.resultCode= resultCode;
  }
 
  public boolean equals(Object id){
    return id != null && id instanceof PKResult &&
           ((PKResult)id).getState().equals(state) &&
           ((PKResult)id).getResultCode() == resultCode;
  }
 
  public int hashCode(){
    return 3*state.getId().getParent().getParentId() + 11*resultCode;
  }

}


And to be more exact, the message error:

Code:
Repeated column in mapping for entity: dbo.Result column: Parent_Id (should be mapped with insert="false" update="false")


But I want to be capable to change the "Area_Code" column with the reference "area" into the class "Result".

That's it!


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