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!