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

标签 java jpa jpa-2.0

This is my Entity class

 @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);
}

This is my Embeddable class

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

This is my main class how set the composite values and why cant we use generatedvalue for autoincrement purpose and how to retrieve the values from the the database and one more thing where to declare other fields in Entity class or embeddable class and if not how to set and get the values from these 2 classes(entity and 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();
        }

最佳答案

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

关于java - 如何在jpa中设置和获取复合主键和其他字段的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22168803/

相关文章:

java - 如何在生成的规范元数据类中抑制原始类型警告?

Java MS SQL -> mySQL 转换

java - 批量插入现有数据 : Preventing JPA to do a select before every insert

java - Hibernate 5 创建 ID 未标记为 IDENTITY 的表

java - Spring 数据 JPA : How to write subquery having IN operator

java - 具有 Activity 事务和连接打开的 LazyInitializationException

java - 使用 MySQL 的简单 Java EE 客户端桌面应用程序和 Web

java - 用于对 ArrayList 进行排序的 Java 内联比较器的问题

java - 从innerHTML获取未知长度的特定字符串

hibernate - javax.persistence.PersistenceException - JPA+ hibernate