这是我的实体类
@EmbeddedId
private AuthorWorkPKEmbedded embeddedId;
@Column(name = "ColumnA")
private String ColumnA;
public AuthorWorkPKEmbedded getEmbeddedId() {
return embeddedId;
}
public void setEmbeddedId(AuthorWorkPKEmbedded embeddedId) {
this.embeddedId = embeddedId;
}
public String getColumnA() {
return ColumnA;
}
public void setColumnA(String ColumnA) {
this.ColumnA = ColumnA;
}
public AuthorWorkEmbedded() {
}
public AuthorWorkEmbedded(BigInteger bookId,BigInteger authorId) {
this.embeddedId = new AuthorWorkPKEmbedded(bookId, authorId);
}
这是我的Embeddable类
@Embeddable
@Column(name = "bookId", nullable = false)
private BigInteger bookId;
@Column(name = "authorId", nullable = false)
private BigInteger authorId;
public AuthorWorkPKEmbedded() {
}
public AuthorWorkPKEmbedded(BigInteger bookId, BigInteger authorId) {
this.bookId = bookId;
this.authorId = authorId;
}
public BigInteger getBookId() {
return bookId;
}
public void setBookId(BigInteger bookId) {
this.bookId = bookId;
}
public BigInteger getAuthorId() {
return authorId;
}
public void setAuthorId(BigInteger authorId) {
this.authorId = authorId;
}
@Override
public int hashCode() {
return bookId.hashCode() + authorId.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof AuthorWorkPKEmbedded)) {
return false;
}
if (obj == null) {
return false;
}
AuthorWorkEmbedded pk=(AuthorWorkEmbedded) obj;
return (((bookId==((AuthorWorkPKEmbedded)obj).getBookId()))
&&((authorId==((AuthorWorkPKEmbedded)obj).getAuthorId())));
}
这是我的主要课程 如何设置复合值以及为什么我们不能将generatedvalue用于自动增量目的以及如何从数据库中检索值以及在Entity类或embeddable类中声明其他字段的另一个方法,如果不是如何设置和获取值这两个类(实体和嵌入式)
EntityTransaction entr = em.getTransaction();
entr.begin();
AuthorWorkPKEmbedded author = new AuthorWorkPKEmbedded();
author.setBookId(BigInteger.ONE);
author.setAuthorId(BigInteger.ONE);
AuthorWorkEmbedded a1=new AuthorWorkEmbedded();
a1.setEmbeddedId(author);
a1.setColumnA("Pirates of carrabian");
boolean successful = false;
try {
em.persist(author);
successful = true;
} finally {
if (successful) {
entr.commit();
} else {
entr.rollback();
}
}
Query query = em.createNamedQuery("AuthorWork.findAll");
List authorList = query.getResultList();
Iterator authorIterator = authorList.iterator();
while (authorIterator.hasNext()) {
author = (AuthorWorkPKEmbedded) authorIterator.next();
System.out.println("Book Id " + author.getBookId() + " " + "Author" + author.getAuthorId() + "");
System.out.println();
}
答案 0 :(得分:0)
使用embeddedId的getter和setter。
Query query = em.createNamedQuery("AuthorWork.findAll");
List authorList = query.getResultList();
Iterator authorIterator = authorList.iterator();
while (authorIterator.hasNext()) {
author = (AuthorWorkEmbedded) authorIterator.next();
System.out.println("Book Id " + author.setEmbeddedId().getBookId() + " " + "Author" + author.getEmbeddedId().getAuthorId() + "");
System.out.println(""+author.getColumnA());
}