Reputation: 745
I have a parent class which is a Visitor class which will have all the fields of a visitor. The class is as follows.
package classes.trantables;
import java.io.Serializable;
import java.sql.Time;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Lob;
import javax.persistence.Table;
import java.sql.Date;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;
@Entity
@Table(name="VISITORS")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Visitors implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@Column(nullable=false,name="COMPANY_ID")
private String companyId;//Maps to company
@Id
@Column(nullable=false,name="BRANCH_ID")
private String branchId;//number of branches
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((branchId == null) ? 0 : branchId.hashCode());
result = prime * result
+ ((companyId == null) ? 0 : companyId.hashCode());
result = prime * result + ((passNo == null) ? 0 : passNo.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Visitors)) {
return false;
}
Visitors other = (Visitors) obj;
if (branchId == null) {
if (other.branchId != null) {
return false;
}
} else if (!branchId.equals(other.branchId)) {
return false;
}
if (companyId == null) {
if (other.companyId != null) {
return false;
}
} else if (!companyId.equals(other.companyId)) {
return false;
}
if (passNo == null) {
if (other.passNo != null) {
return false;
}
} else if (!passNo.equals(other.passNo)) {
return false;
}
return true;
}
@Id
@Column(nullable=false,name="PASS_NO")
private String passNo;//Random generated
@Column(nullable=false)
private String visitorName;//Name given by the visitor
//remainder of the class is not required..
Now I want to create a class which is a child class to this class which stores all the belongings of the visitor. I want the COMPANY_ID, BRANCH_ID,PASS_NO, from the VISITOR class and ItemName from the Belongings class to be the primary key for the Belongings class. I want a table per sub class but I am getting errors if i put the above class in mapping.
This is what I have tried, below..
package classes.trantables;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.PrimaryKeyJoinColumns;
import javax.persistence.Table;
@Entity
@Table(name="BELONGINGS")
@PrimaryKeyJoinColumns({@PrimaryKeyJoinColumn(name="COMPANY_ID",referencedColumnName="COMPANY_ID"),
@PrimaryKeyJoinColumn(name="BRANCH_ID",referencedColumnName="BRANCH_ID"),
@PrimaryKeyJoinColumn(name="PASS_NO", referencedColumnName="PASS_NO")
})
public class Belongings extends Visitors{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
private String itemNm;
private String model;
private String srNo;
Upvotes: 2
Views: 8394
Reputation: 1758
You shouldn't have another @Id
annotated property in your subclass. Removing @Id
from itemNm
in Belongings
will get rid this Exception.
Upvotes: 5