gpt4 book ai didi

java - hibernate :无法删除|删除分离的实例

转载 作者:行者123 更新时间:2023-12-02 11:45:16 25 4
gpt4 key购买 nike

我正在制作一个简单的 api,可以在其中存储图像并对其执行增删改查功能。为了与数据库通信,我使用 Hibernate。虽然获取数据进展顺利,但我无法添加或删除数据。这将导致添加出现此异常;

org.hibernate.exception.GenericJDBCException: could not execute statement

以及删除的异常(exception)情况;

Removing a detached instance com.mycompany.server.model.Image#0

我不知道为什么它不能完成这项工作。它以前有效,我没有对其进行任何更改。希望你能找出问题所在。如果此信息还不够,请评论您想查看的内容。

这里有模型:

@Entity
@Table(name = "image")
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@NamedQueries({
@NamedQuery(name = "Image.findAll", query = "SELECT i FROM Image i")
, @NamedQuery(name = "Image.findByImageId", query = "SELECT i FROM Image i WHERE i.imageId = :imageId")
, @NamedQuery(name = "Image.findByContenttype", query = "SELECT i FROM Image i WHERE i.contenttype = :contenttype")
, @NamedQuery(name = "Image.findByName", query = "SELECT i FROM Image i WHERE i.name = :name")
, @NamedQuery(name = "Image.findByDescription", query = "SELECT i FROM Image i WHERE i.description = :description")})
public class Image implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@XmlElement(nillable=true)
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Basic(optional = false)
@NotNull
@Column(name = "image_id")
private Integer imageId;

@Basic(optional = false)
@NotNull
@Lob
@Size(min = 1, max = 16777215)
@Column(name = "content")
private String content;

@Basic(optional = false)
@Size(min = 1, max = 45)
@Column(name = "contenttype")
private String contenttype;

@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "name")
private String name;

@Size(max = 45)
@Column(name = "description")
private String description;

public Image() {
}

public Image(Integer imageId) {
this.imageId = imageId;
}

public Image(Integer imageId, String content, String contenttype, String name) {
this.imageId = imageId;
this.content = content;
this.contenttype = contenttype;
this.name = name;
}

public Integer getImageId() {
return imageId;
}

public void setImageId(Integer imageId) {
this.imageId = imageId;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public String getContenttype() {
return contenttype;
}

public void setContenttype(String contenttype) {
this.contenttype = contenttype;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

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

@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Image)) {
return false;
}
Image other = (Image) object;
if ((this.imageId == null && other.imageId != null) || (this.imageId != null && !this.imageId.equals(other.imageId))) {
return false;
}
return true;
}

@Override
public String toString() {
return "com.mycompany.server.model.Image[ imageId=" + imageId + " ]";
}

}

以及服务实现:

public class ImageRepositoryServiceImpl implements ImageRepositoryService    {

private EntityManagerFactory entityManagerFactory;

private static ImageRepositoryServiceImpl instance;

private ImageRepositoryServiceImpl() {
entityManagerFactory = Persistence.createEntityManagerFactory("com.mycompany_server_war_1.0-SNAPSHOTPU");
}

static {
instance = new ImageRepositoryServiceImpl();
}

private EntityManager getEntityManager() {
return entityManagerFactory.createEntityManager();
}

public static ImageRepositoryService getInstance() {
return instance;
}

@Override
public List<Image> getAllImages() {
EntityManager em = entityManagerFactory.createEntityManager();
List<Image> images = em.createNamedQuery("Image.findAll", Image.class).getResultList();
em.close();
return images;
}

@Override
public Image getImageFromId(int imageId) {
EntityManager em = entityManagerFactory.createEntityManager();
Image image = em.find(Image.class, imageId);
em.close();
return image;
}

@Override
public boolean addImage(Image image) {
try{
EntityManager em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
em.persist(image);
em.getTransaction().commit();
em.close();
return true;
}catch(Exception e){
return false;
}
}

@Override
public boolean editImage(Image image) {
throw new UnsupportedOperationException("Not supported yet. edit"); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean DeleteImage(Image image) {
try{
EntityManager em = entityManagerFactory.createEntityManager();
em.remove(image);
return true;
}catch(Exception e){
return false;
}
}

}

编辑:

我解决了添加问题。显然 image_id 没有自动增加。现在我换成ai了,效果还不错。但删除仍然不起作用:'(

最佳答案

我在这段代码中看到了什么问题:

  1. 您滥用实体管理器。每个实体应该属于一个实体管理者。如果您使用一个实体管理器加载实体,然后使用另一个实体管理器保存它,您可能会收到诸如正在删除分离的实例 com.mycompany.server.model.Image#0 之类的错误。
  2. 只有在没有异常的情况下,才可以调用实体管理器的close。如果在调用 close 之前出现异常,您将导致资源泄漏。尝试使用 try-with-resources。
  3. 您根据生成的标识符为实体实现了 equals 和 heshCode。这是不好的做法。如果您没有其他 Image 相等标准 - 请勿覆盖 equalshashCode

也许这篇笔记会对您有所帮助。

关于java - hibernate :无法删除|删除分离的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48255159/

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