每个主题,我有一个实体A,比如说a1(PK),a2。 实体B,列a2(PK),b1 实体C,列b1(PK),c1
所以用a2连接到B,然后用b1连接到C. (它们都是一对一的关系)
这些实体在web服务中使用,当被调用时将生成包含这些实体列表的XML文件。 现在生成一个XML,将实体A和B与字段a1,a2,a1组合在一起非常简单。
但是,如果我想生成仅显示实体A和实体C的XML,那么需要什么语法/注释?我的意思是,我如何设置该实体A以便它可以实现?
如果我可以从实体A,B,C获得所有字段,我也会解决。
我正在使用jpa2,ejb3.1
修改:添加示例。
实体A
@Entity
@Table(name = "TABLE_A")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "MyComplex.findAll", query = "SELECT j FROM MyComplex j"),
@NamedQuery(name = "MyComplex.findA1", query = "SELECT j FROM MyComplex j WHERE j.a1 = :A1")
})
public class MyComplex implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "A1", length = 1)
private String a1;
@Column(name = "A2", length = 1)
private String a2;
public MyComplex() {
}
public MyComplex(String a1) {
this.a1 = a1;
}
public String getA1() {
return a1;
}
public void setA1(String a1) {
this.a1 = a1;
}
public String getA2() {
return a2;
}
public void setA2(String a2) {
this.a2 = a2;
}
@Override
public int hashCode() {
int hash = 0;
hash += (a1 != null ? a1.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
if (!(object instanceof MyComplex)) {
return false;
}
MyComplex other = (MyComplex) object;
if ((this.a1 == null && other.a1 != null) || (this.a1 != null && !this.a1.equals(other.a1))) {
return false;
}
return true;
}
@Override
public String toString() {
return "jpa.mypackage.Table_A[a1=" + a1 + " ]";
}
}
实体B
@Entity
@Table(name = "TABLE_B")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Table_B.findAll", query = "SELECT j FROM Table_B j"),
@NamedQuery(name = "Table_B.findA2", query = "SELECT j FROM Table_B j WHERE j.a2 = :A2")
})
public class Table_B implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "A2", length = 1)
private String a2;
@Size(max = 1)
@Column(name = "B1", length = 1)
private String b1;
//entity constructor here
//entity get/set methods here
//entity hash methods here
}
实体C
@Entity
@Table(name = "TABLE_C")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Table_C.findAll", query = "SELECT j FROM Table_C j"),
@NamedQuery(name = "Table_C.findB1", query = "SELECT j FROM Table_C j WHERE j.b1 = :B1")
})
public class Table_C implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "B1", length = 1)
private String b1;
@Size(max = 1)
@Column(name = "C1", length = 1)
private String c1;
//entity constructor here
//entity get/set methods here
//entity hash methods here
}
修改实体A以加入实体B
@Entity
@Table(name = "TABLE_A")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "MyComplex.findAll", query = "SELECT j FROM MyComplex j"),
@NamedQuery(name = "MyComplex.findA1", query = "SELECT j FROM MyComplex j WHERE j.a1 = :A1")
})
public class MyComplex implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "A1", length = 1)
private String a1;
@Column(name = "A2", length = 1)
private String a2;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="A2", referencedColumnName="A2")
private Table_B table_B;
//entity constructor here
//entity get/set methods here
public Table_B getTable_B() {
return table_B;
}
public void setTable_B(Table_B table_B) {
this.table_B = table_B;
}
//entity hash methods here
}
答案 0 :(得分:0)
根据你的图,我看到Table_B只是A和C的连接表。如果在MyComplex.java中使用JoinTable设置OneToOne注释,那么我相信你可以设置Table_A和Table_C的关系。
@Entity
@Table(name = "TABLE_A")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "MyComplex.findAll", query = "SELECT j FROM MyComplex j"),
@NamedQuery(name = "MyComplex.findA1", query = "SELECT j FROM MyComplex j WHERE j.a1 = :A1")
})
public class MyComplex implements Serializable {
/* ------------------------------------ */
@OneToOne
@JoinTable(
name="Table_B",
joinColumns=
@JoinColumn(name="A2", referencedColumnName="A2"),
inverseJoinColumns=
@JoinColumn(name="B1", referencedColumnName="B1")
)
private Table_C tableC;
/* ------------ */
}