错误 - 具有级联=" all-delete-orphan"的集合所拥有的实体实例

时间:2017-07-26 13:47:22

标签: java jpa gwt

现在发生了这个错误。

cascade =" all-delete-orphan"不再由拥有实体实例引用:entity.TestMaster.uploadedFileList

请给我一个建议。

public void setUploadedFileList(List<UploadedFile> uploadedFileList) {

    this.uploadedFileList.clear();
    this.uploadedFileList.addAll(uploadedFileList);
}

我改变了那样,但又出现了错误

TestMaster.java

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE  , generator = "MYSEQONE" )
@SequenceGenerator(name="MYSEQONE", sequenceName = "test_sequence" , allocationSize=1)
@Column(length = 10 )
private int testId;

@OneToMany(cascade = { CascadeType.ALL }, orphanRemoval = true)
private List<UploadedFile> uploadedFileList;


@Column(length = 300)
private String pageHead;

@Column(length = 300)
private String pageFooter;



public int getTestId() {
    return testId;
}

public void setTestId(int testId) {
    this.testId = testId;
}

public List<UploadedFile> getUploadedFileList() {
    return uploadedFileList;
}

public void setUploadedFileList(List<UploadedFile> uploadedFileList) {
    this.uploadedFileList = uploadedFileList;

/*  
    this.uploadedFileList.clear();
    this.uploadedFileList.addAll(uploadedFileList);
*/

}

public String getPageHead() {
    return pageHead;
}

public void setPageHead(String pageHead) {
    this.pageHead = pageHead;
}

public String getPageFooter() {
    return pageFooter;
}

public void setPageFooter(String pageFooter) {
    this.pageFooter = pageFooter;
}

UploadedFile.java

@Entity
@Table(
    indexes = {@Index(columnList = "path")}
)
public class UploadedFile implements Serializable, IsSerializable, HasProperty
{
    private static final long serialVersionUID = 748467193701593168L;

    @Id
    @GeneratedValue
    private long seq;

    @Column(length = 100)
    private String name;

    @Column(updatable = false)
    private long size;

    @Column(length = 10, updatable = false)
    private String ext;

    @Column(length = 100)
    private String fileName;

    @Column(length = 100, updatable = false)
    private String mimeType;

    @Column(length = 50, updatable = false)
    private String path;

    @Transient
    private String data; // data transferred from client

    public UploadedFile() {}

    public UploadedFile(File file, String data) {
        this.name = file.getName();
        this.size = file.getSize();
        this.mimeType = file.getType();
        this.data = data;

        if (name != null) {
            int idx = name.lastIndexOf('.');
            if (idx >= 0) {
                this.ext = name.substring(idx + 1);
                this.name = name.substring(0, idx);
            }
        }
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getName() {
        return name;
    }

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

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public String getMimeType() {
        return mimeType;
    }

    public void setMimeType(String mimeType) {
        this.mimeType = mimeType;
    }

    public long getSeq() {
        return seq;
    }

    public void setSeq(long seq) {
        this.seq = seq;
    }

    public String getExt() {
        return ext;
    }

    public void setExt(String ext) {
        this.ext = ext;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

@Override
public Object getProperty(String name) {
    if ("seq".equals(name)) return this.seq;
    else if ("name".equals(name)) return this.name;
    else if ("fileName".equals(name)) return this.fileName;
    else if ("size".equals(name)) return this.size;
    else if ("ext".equals(name)) return this.ext;
    else if ("mimeType".equals(name)) return this.mimeType;
    else if ("path".equals(name)) return this.path;
    else if ("data".equals(name)) return this.data;
    else return null;
}

@Override
public void setProperty(String name, Object val) {
    if ("seq".equals(name)) this.seq = PropUtil.toLong(val);
    else if ("name".equals(name)) this.name = PropUtil.toString(val);
    else if ("fileName".equals(name)) this.fileName = PropUtil.toString(val);
    else if ("size".equals(name)) this.size = PropUtil.toLong(val);
    else if ("ext".equals(name)) this.ext = PropUtil.toString(val);
    else if ("mimeType".equals(name)) this.mimeType = PropUtil.toString(val);
    else if ("path".equals(name)) this.path = PropUtil.toString(val);
    else if ("data".equals(name)) this.data = PropUtil.toString(val);

}


private static final char[] BASE64_CHARS = {
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
    'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
    'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
    'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
    'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
    'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
    '8', '9', '+', '/'
};
private static final char BASE64_PADDING = '=';

public static String toBase64(String array) {
    StringBuilder builder = new StringBuilder();
    int length = array.length();
    if (length > 0) {
        char[] charArray = new char[4];
        int ix = 0;
         while (length >= 3) {
            int i = ((array.charAt(ix) & 0xff)<<16)
                + ((array.charAt(ix+1) & 0xff)<<8)
                + (array.charAt(ix+2) & 0xff);
            charArray[0] = BASE64_CHARS[i>>18];
            charArray[1] = BASE64_CHARS[(i>>12) & 0x3f];
            charArray[2] = BASE64_CHARS[(i>>6) & 0x3f];
            charArray[3] = BASE64_CHARS[i & 0x3f];
            builder.append(charArray);
            ix += 3;
            length -= 3;
        }
        if (length == 1) {
            int i = array.charAt(ix)&0xff;
            charArray[0] = BASE64_CHARS[i>>2];
            charArray[1] = BASE64_CHARS[(i<<4)&0x3f];
            charArray[2] = BASE64_PADDING;
            charArray[3] = BASE64_PADDING;
            builder.append(charArray);
        } else if (length == 2) {
            int i = ((array.charAt(ix) & 0xff)<<8)
                + (array.charAt(ix+1) & 0xff);
            charArray[0] = BASE64_CHARS[i>>10];
            charArray[1] = BASE64_CHARS[(i>>4) & 0x3f];
            charArray[2] = BASE64_CHARS[(i<<2) & 0x3f];
            charArray[3] = BASE64_PADDING;
            builder.append(charArray);
        }
    }
    return builder.toString();
}

}

TestMasterActivity.java

private TestMasterServiceAsync testMasterService;


public void insertShopInfo(  TestMaster test , final Place place )
{
    Toast.showLoading();
    test.setUserId( ClientContext.getLoginUser().getUserId() );
    test.setRegUserId( ClientContext.getLoginUser().getUserId() );
    test.setRegDate(new Date());
    test.setDelFlg("0");


    testMasterService.insertTest( test, new SwabAdminCallback<Void>() {

        @Override
        public void onSuccess(Void result)
        {
            Toast.showInfo("$systxt.saved");
            goTo(place);
        }
    });
}

TestServiceImpl.java

private String headerString = "https://dummyimage.com/960x110/000000/ff4545.jpg&text=Header";
private String footerString = "https://dummyimage.com/320x120/000000/ff4545.jpg&text=Footer";

@PersistenceContext
private EntityManager em;

@Override
public void insertTest(TestMaster obj) throws SwabException {


    if(obj.getPageHead()==null && obj.getPageFooter()==null){

        obj.setPageHead(headerString);
        obj.setPageFooter(footerString);

    }else if(obj.getPageHead()==null && obj.getPageFooter().length()>0){

        obj.setPageHead(headerString);

    }else if(obj.getPageHead().length()>0 && obj.getPageFooter()==null){

        obj.setPageFooter(footerString);
    }


    if(obj.getPageHead().length()>0 && obj.getPageHead() != headerString){

        em.clear();

        Query keyq = em.createQuery("select path from UploadedFile where fileName = :fileName");


        keyq.setParameter("fileName", obj.getPageHead());

        String filePath = null;

        filePath = (String) keyq.getSingleResult();

        obj.setPageHead(filePath);

    }


    if(obj.getPageFooter().length()>0 && obj.getPageFooter() != footerString ){

        em.clear();

        Query keyq2 = em.createQuery("select path from UploadedFile where fileName = :fileName2");

        keyq2.setParameter("fileName2", obj.getPageFooter());

        String filePath2 = null;

        filePath2 = (String) keyq2.getSingleResult();

        obj.setPageFooter(filePath2);

    }

    TestMaster searchData = em.find(TestMaster.class, obj.getTestId;());

    if (searchData == null) {
        logger.debug("insert");
        em.persist(obj);
    } else {
        logger.debug("update");
        em.merge(obj);
    }

}

0 个答案:

没有答案