How to set and get values for Composite primary keys and other fields in jpa?

StackOverflow https://stackoverflow.com/questions/22168803

  •  02-06-2023
  •  | 
  •  

Вопрос

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();
        }
Это было полезно?

Решение

Use getters and setters for embeddedId .

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());
        }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top