我有一个父类,它是一个访客类,它将包含访问者的所有字段。课程如下。
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..
现在我想创建一个类,它是这个类的子类,它存储访问者的所有所有物。 我希望来自VISITOR类的COMPANY_ID,BRANCH_ID,PASS_NO和来自Belongings类的ItemName成为Belongings类的主键。我想要每个子类一个表,但如果我将上面的类放在映射中,我会收到错误。
这就是我在下面试过的..
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;
答案 0 :(得分:5)
您的子类中不应该有另一个@Id
带注释的属性。从@Id
中itemNm
移除Belongings
将取消此例外。