gpt4 book ai didi

java - entityManager.find 和 entityManager.createQuery 有什么区别?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:27:08 30 4
gpt4 key购买 nike

好的,我正在使用 EJB 3.0 和 hibernate,我们将 .ear 文件放入嵌入 Apache Tomcat 6.0.18 的 Easy-Beans 1.0.1(带有 Hibernate)部署目录。所以我的数据库必须保留这样的东西:

@Entity
@Table(name="AUTHOR")
public class Author implements Serializable {

//////////////////////////////////////////////////////////////////
// Fields
//////////////////////////////////////////////////////////////////

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "A_ID", unique=true, nullable = false)
private Integer id;

@Column (name = "A_NAME", unique = false, nullable = false)
private String name;

@Column (name = "A_LASTNAME", unique = false, nullable = false)
private String lastname;

@OneToMany(cascade = {ALL}, fetch = EAGER, mappedBy = "author")
private Set<BookAuthor> bookAuthors = new HashSet<BookAuthor>();

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Author author = (Author) o;

if (id != null ? !id.equals(author.id) : author.id != null) return false;

return true;
}

@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
}

@Entity
@Table(name = "BOOK" )
public class Book implements Serializable {
//////////////////////////////////////////////////////////////////
// Fields
//////////////////////////////////////////////////////////////////
@Id
@GeneratedValue (strategy = IDENTITY)
@Column(name = "B_ID", unique = true, nullable = false)
private Integer bid;

@Column(name = "B_YEAR", unique = false, nullable = true)
private Integer year;

@Column(name = "B_ISBN", unique = false, nullable = false)
private String isbn;

@Column(name = "B_TITLE", unique = false, nullable = false)
private String title;

@OneToMany(cascade = {ALL}, fetch = EAGER, mappedBy = "book")
private Set<BookAuthor> bookAuthors = new HashSet<BookAuthor>();

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Book book = (Book) o;

if (isbn != null ? !isbn.equals(book.isbn) : book.isbn != null) return false;
if (bid != null ? !kid.equals(book.bid) : book.bid != null) return false;

return true;
}

@Override
public int hashCode() {
int result = bid != null ? bid.hashCode() : 0;
result = 31 * result + (isbn != null ? isbn.hashCode() : 0);
return result;
}

}

@Entity
@Table(name = "BOOK_AUTHOR")
public class BookAuthor implements Serializable {

//////////////////////////////////////////////////////////////////
// Fields
//////////////////////////////////////////////////////////////////
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "BA_ID", unique=true, nullable = false)
private Integer id;

@Column(name = "BA_ROLE", unique = false, nullable = true)
private String role;

@ManyToOne
@JoinColumn (name = "A_ID", referencedColumnName = "A_ID", nullable = false)
private Author author;

@ManyToOne
@JoinColumn (name = "B_ID", referencedColumnName = "B_ID", nullable = false)
private Book book;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

BookAuthor that = (BookAuthor) o;

if (auhtor != null ? !author.equals(that.author) : that.author != null) return false;
if (book != null ? !book.equals(that.book) : that.book!= null) return false;
if (id != null ? !id.equals(that.id) : that.id != null) return false;

return true;
}

@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (author != null ? author.hashCode() : 0);
result = 31 * result + (book != null ? book.hashCode() : 0);
return result;
}

}

所以当删除项目时,我有一个实体 bean,它是这样的:

@Stateless
@Local(DeleteBookAuthor.class)
public class DeleteBookAuthorBean implements DeleteBookAuthor
{

@PersistenceContext(unitName="Library")
protected EntityManager em;

@Override
public void removeById(Integer id) {
try{

Query q = em.createQuery("SELECT ba FROM BookAuthor ba WHERE id = ?1");
q.setParameter(1,id);

BookAuthor ba = (BookAuthor) q.getSingleResult();

ba.getAuthor().getBookAuthors().remove(ba);
ba.getBook().getBookAuthors().remove(ba);

em.remove(ba);
}catch (Exception e){
e.printStackTrace();
}
}
}

不幸的是,当我的 Servlet 调用这个 bean 时,它返回一个“已删除的实体传递给持久化”异常;但是改变行:

Query q = em.createQuery("SELECT ba FROM BookAuthor ba WHERE id = ?1");
q.setParameter(1,id);
BookAuthor ba = (BookAuthor) q.getSingleResult();

BookAuthor ba = em.find(BookAuthor.class, id)

让问题消失。我问的问题是为什么?在类似的情况下,我使用 em.createQuery 来检索和删除多个实体并且它工作顺利。那为什么它现在不起作用了?

更新:调用查询 q=...,然后删除 BookAuthors 会从 Books 中删除 BookAuthors,但不会从 Authors 中删除。在第二种情况下,它被从两个集合中移除。当使用 baQuery.equals(baFind) 进行比较时,两个 ba 都具有相同的哈希并返回 true

为什么两个函数会返回相同的对象,但调用 remove 会根据是否调用 query/find 表现不同,是否有任何说明?

最佳答案

可能与BookAuthor中缺少equals()/hashCode()有关。如果是这样,在查询的情况下,您似乎有多个具有相同状态的 BookAuthor 实例,因此它们不会从 Author

关于java - entityManager.find 和 entityManager.createQuery 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4339513/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com