JPA和Java SE - 由于其他便利方法,未为Entity类创建表

时间:2017-09-08 12:15:10

标签: java jpa eclipselink

我目前正在编写一个用于持久性的Netbeans平台应用程序我使用JPA 2.1和eclipse链接作为提供程序。我有以下实体类:

import java.util.UUID;
import javafx.beans.property.SimpleStringProperty;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

/**
 *
 * @author forell
 */
@Entity
@Access(AccessType.FIELD)
public class MaterialAbc {
    @Id
    private String id;

//... several additional fields, that all work fine


    private String substanceInstances = "";


    public MaterialAbc(){
        id = UUID.randomUUID().toString();
    }

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }


//.. getters and setters for the omitted fields        
    public String getSubstanceInstances() {
        return substanceInstances;
    }

    public void setSubstanceInstances(String substanceInstances) {
        this.substanceInstances = substanceInstances;
    }

    /*  IF I UNCOMMENT THESE LINES THE DATATABLE IS NOT CREATED ANYMORE
        IS THERE A WAY TO SOLVE THIS?

    public List<SubstanceInstance2> getSubs() {
        List<SubstanceInstance2> subs = new ArrayList<>();

        if (!"".equals(substanceInstances)){

            List<String> values = Arrays.asList(substanceInstances.split("|"));
            values.forEach(value->{
                subs.add(SubstanceInstance2.unSerialize(value));
            });

        }
        return subs;
    }
    public void setSubs(List<SubstanceInstance2> subs) {
        substanceInstances = "";
        subs.forEach(sub->{
            substanceInstances =substanceInstances + sub.serialize() + "|";
        });
        substanceInstances = substanceInstances.substring(0, substanceInstances.length()-1);
    }
*/

由于该类工作正常,只要我取消注释底部的两个方法“反序列化”嵌套在字符串physicalInstances中的对象,eclipselink就不再创建数据表了。有没有办法解决这个问题,或者需要添加额外的图层。

1 个答案:

答案 0 :(得分:0)

与此同时,我实际上找到了解决问题的方法。如果方法中使用了lambda表达式,那么eclipselink似乎不会将Entity-bean转换为表。最后我转换了

values.forEach(value->{
    subs.add(SubstanceInstance2.unSerialize(value));
});

进入

for (String value:values){
    subs.add(SubstanceInstance2.unSerialize(value));
}

并且很好地完成了工作。至于原因,不知道!