Java 如何设置和获取jpa中复合主键和其他字段的值?

Java 如何设置和获取jpa中复合主键和其他字段的值?,java,jpa,jpa-2.0,Java,Jpa,Jpa 2.0,这是我的实体类 这是我的可嵌入类 这是我的主课 如何设置复合值,为什么我们不能使用generatedvalue进行自动增量,如何从数据库中检索值,还有一件事是在实体类或可嵌入类中声明其他字段,如果不是,如何从这两个类(实体和可嵌入)中设置和获取值 对embeddedId使用getter和setter Query query = em.createNamedQuery("AuthorWork.findAll"); List authorList = query.getResultL

这是我的实体类

这是我的可嵌入类

这是我的主课 如何设置复合值,为什么我们不能使用generatedvalue进行自动增量,如何从数据库中检索值,还有一件事是在实体类或可嵌入类中声明其他字段,如果不是,如何从这两个类(实体和可嵌入)中设置和获取值


对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());
        }

:我已按照您的要求在entity类中设置和获取AuthorWorkEmbedded类中声明其他字段。@Embeddeble类中只应存在PK文件。:author.setEmbeddedId().getBookId()我无法使用setEmbeddedId,因为它是实体类。你能告诉我如何处理吗抱歉..保存前设置的是正确的。只有在获取数据库值时才使用System.out.println(“Book Id”+author.getEmbeddedId().getBookId()+“”+author”+author.getEmbeddedId().getAuthorId()+“”);System.out.println(“+author.getColumnA());持久化em.persist(a1)的值;您应该保存实体类而不是可嵌入类。并获取已保存实体类的值。
 @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())));
}
 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();
        }
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());
        }