涉及联合子类继承的一对一关联
在开发基于 JSF 的项目时,我遇到了 Hibernate 继承问题。起初,我倾向于使用由两个具体类实现的接口,但由于不可能映射接口,所以我使用了由两个具体类扩展的抽象类。在代码方面:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class AbstractEntity implements Serializable {
protected Long id;
@Id
@GeneratedValue(strategy=GenerationType.TABLE)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
@Entity
@PrimaryKeyJoinColumn(name="id_company")
public class Company extends AbstractEntity {
private Individal signer;
private Individuo billHolder;
private String partIva;
@OneToOne
public Individual getSigner() {
return signer;
}
public void setSigner(Individual signer) {
this.signer = signer;
}
@OneToOne
public Individual getBillHolder() {
return billHolder;
}
public void setBillHolder(Individual billHolder) {
this.billHolder = billHolder;
}
public String getPartIva() {
return partIva;
}
public void setPartIva(String partIva) {
this.partIva = partIva;
}
}
@Entity
@PrimaryKeyJoinColumn(name="id_private")
public class Private extends AbstractEntity {
private Individual billHolder;
@OneToOne
public Individual getBillHolder() {
return billHolder;
}
public void setBillHolder(Individual billHolder) {
this.billHolder = billHolder;
}
@OneToOne
public Individual getSigner() {
return billHolder;
}
public void setSigner(Individual signer) {
}
}
现在我有第四个类(名为“Client”),它与 AbstractEntity 具有一对一的单向关系:
@Entity
public class Client implements Serializable {
private Long id;
private AbstractEntity holder;
...
@OneToOne
public SoggettoAstratto getHolder() {
return holder;
}
public void setHolder(SoggettoAstratto cliente) {
this.intestatario = cliente;
}
...
Individual 是一个没有关系和属性等的描述性类,称为 CodFis。 现在,如果我使用此条件进行查询:
Criteria crit = super.getSession().createCriteria(getPersistentClass());
crit.createCriteria("holder.billHolder")
.add(Restrictions.eq("codFis", codFis));
List risultati = crit.list();
*NOTE: the code is part of a DAO. The getPersistentClass returns a Client.class, and it works perfectly with other methods in this DAO.*
我得到此异常:
org.postgresql.util.PSQLException: ERROR: table name "individual1_" specified more than once
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2062)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1795)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:479)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:367)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:271)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1787)
我无法弄清楚问题是什么。有谁这样做吗?
提前致谢。 JLPicard
P.S. 属性名称是从我的语言翻译而来的,因此它们的意思与英语不完全匹配。
I'm stucked on a Hibernate inheritance problem while working on a JSF-based. At first, I was oriented to use a interface implemented by two concrete classes but since mapping interfaces is not possible I used an abstract class extended by the two concrete classes. In terms of code:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class AbstractEntity implements Serializable {
protected Long id;
@Id
@GeneratedValue(strategy=GenerationType.TABLE)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
@Entity
@PrimaryKeyJoinColumn(name="id_company")
public class Company extends AbstractEntity {
private Individal signer;
private Individuo billHolder;
private String partIva;
@OneToOne
public Individual getSigner() {
return signer;
}
public void setSigner(Individual signer) {
this.signer = signer;
}
@OneToOne
public Individual getBillHolder() {
return billHolder;
}
public void setBillHolder(Individual billHolder) {
this.billHolder = billHolder;
}
public String getPartIva() {
return partIva;
}
public void setPartIva(String partIva) {
this.partIva = partIva;
}
}
@Entity
@PrimaryKeyJoinColumn(name="id_private")
public class Private extends AbstractEntity {
private Individual billHolder;
@OneToOne
public Individual getBillHolder() {
return billHolder;
}
public void setBillHolder(Individual billHolder) {
this.billHolder = billHolder;
}
@OneToOne
public Individual getSigner() {
return billHolder;
}
public void setSigner(Individual signer) {
}
}
Now I have a fourth class (named "Client") that has a One-to-One monodirectional relation with AbstractEntity:
@Entity
public class Client implements Serializable {
private Long id;
private AbstractEntity holder;
...
@OneToOne
public SoggettoAstratto getHolder() {
return holder;
}
public void setHolder(SoggettoAstratto cliente) {
this.intestatario = cliente;
}
...
And Individual is a descriptive class without relations and a property, among other, called CodFis.
Now if I a query using this criteria:
Criteria crit = super.getSession().createCriteria(getPersistentClass());
crit.createCriteria("holder.billHolder")
.add(Restrictions.eq("codFis", codFis));
List risultati = crit.list();
*NOTE: the code is part of a DAO. The getPersistentClass returns a Client.class, and it works perfectly with other methods in this DAO.*
I get this exception:
org.postgresql.util.PSQLException: ERROR: table name "individual1_" specified more than once
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2062)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1795)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:479)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:367)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:271)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1787)
I can't figured out what's the problem. Anyone who does?
Thanks in advance.
JLPicard
P.S.
The property names are translated from my language, so their mean doesn't match exactly in english.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论