【问题标题】:JPA merge is inserting instead of updatingJPA 合并正在插入而不是更新
【发布时间】:2018-02-22 12:01:11
【问题描述】:

我有一个带有字段NAME 的数据库,我需要将图片作为blob 类型上传到NAME 等于文件名的每个实体上的字段PICTURE。但是,在运行代码时,会创建一堆具有重复的 NAME 字段和 PICTURE 字段的新实体。 这是我的代码:

主要:

public static void main(String[] args) {
    File currentDir = new File("C:\\Users\\luccaskammer\\Desktop\\Imagens\\"); // Define o diretorio a ser lido
    displayDirectoryContents(currentDir);
}

public static void displayDirectoryContents(File dir) {
    try {

        ImagensParaDBController contr = new ImagensParaDBController();
        File[] files = dir.listFiles();
        DeletePng del = new DeletePng();
        //ConvertData conv = new ConvertData();

        for (File file: files) {

            ImagensParaBD img = new ImagensParaBD();
            ImagensParaBD name = new ImagensParaBD();
            ImagensParaBD picture = new ImagensParaBD();

            if (file.isDirectory())
            {
                System.out.println("directory:" + file.getCanonicalPath());
                displayDirectoryContents(file);
            }

            else if (file.isFile()) {
                System.out.println("     file:" + file.getCanonicalPath());
                img.setName(del.deletePng(file.getName()));
                java.nio.file.Path filelocation = Paths.get(file.getCanonicalPath());
                img.setPicture(Files.readAllBytes(filelocation));
                contr.insertImageDB(img, picture, name);
            }

            else {
                System.out.println("invalid");
            }
        }

        contr.disconnect();

    }
    catch(IOException e) {}
}

@Entity
@Table(name = "planegeo")

public class ImagensParaBD implements Serializable {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    //private String pathFolder;
    private String name;@Lob
    private byte[] picture;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public byte[] getPicture() {
        return picture;
    }

    public void setPicture(byte[] picture) {
        this.picture = picture;
    }
}

控制器

public class ImagensParaDBController {

    EntityManagerFactory mf = Persistence.createEntityManagerFactory("mySQLPU");
    EntityManager em = mf.createEntityManager();

    public void insertImageDB(ImagensParaBD img, ImagensParaBD name, ImagensParaBD picture) {

        em.getTransaction().begin();
        em.merge(img);
        em.getTransaction().commit();

    }

    public void disconnect() {

        em.close();
    }

    public void insertImageDB(ImagensParaBD img) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

【问题讨论】:

    标签: java sql jpa


    【解决方案1】:

    问题是您创建了新实例。相反,您必须从数据库中检索 name=required 的所有 ImagensParaBD。

    您新创建的实例没有 id,所以 hibernate 只是插入它们。

    List<ImagensParaBD> all = em.createQuery("Select a From ImagensParaBD a where name=:name", 
    ImagensParaBD.class)
    .setParameter("name", theNameTOFind)
    .getResultList();
    

    然后迭代实例添加信息并合并它们

    【讨论】:

    • 感谢您的意见!但是,这对我不起作用。我对代码进行了此更新:code em.getTransaction().begin(); Query queryName= em.createQuery(from table_name where picture = :picture); queryName.setParameter("picture",picture).setParameter("name", name); List&lt;ImagensParaBD&gt; result = queryName.getResultList(); ImagensParaBD imagensParaBD = new ImagensParaBD(); imagensParaBD = result.get(0); em.merge(imagensParaBD); em.getTransaction().commit();
    【解决方案2】:

    JPA 将尝试从 ID 中查找数据库行对象。 如果您的 ID 与数据库 ID 相同,则唯一的 JPA 合并该行。

    这里你只传递图片对象。 所以首先尝试从数据库中查找对象。 如果它存在于数据库中,则在您的对象中获取 ID 和 setID。 我会工作。

    示例代码:

    Query queryName= em.createQuery(from table_name where picture = :picture);
    queryName.setParameter("picture",your picture Object);
    
    list<ImagensParaBD> result = queryName.getResultList();
    ImagensParaBD imagensParaBD = new ImagensParaBD();
    imagensParaBD = result.get(0);
    em.merge(imagensParaBD);
    

    【讨论】:

      猜你喜欢
      • 2011-07-03
      • 1970-01-01
      • 2017-10-27
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      • 2015-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多