簡體   English   中英

JPA 合並是插入而不是更新

[英]JPA merge is inserting instead of updating

我有一個帶有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.
    }
}

問題是您創建了新實例。 相反,您必須從 DB 中檢索 name=required 的所有 ImagensParaBD。

您新創建的實例沒有 id,因此休眠只是插入它們。

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

然后迭代添加信息並合並它們的實例

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM