JPA 2.0: @AttributeOverrides和继承属性之间的关系不太好。

时间:2022-09-20 14:11:36

I need help for the next problem which seems quite similar to this one. However, the suggested solution does not work in my case. I have the class RoadDistance that is tagged as @Embbedable and the class RoadMetricLoader, that is @Embbedable too and contains two attributes of type RoadDistante. There is also the class RoadConnection, which is an Entity and includes an attribute of the type RoadmetricLoader. I am not sucessful in overriding the attributes (@AttributeOverride) of RoadMetricLoader for the class RoadDistance (I do not get the fields ROAD_ESTIMATED_DISTANCE_VALUE, ROAD_ESTIMATED_DISTANCE_UNIT_ID, ROAD_REAL_DISTANCE_VALUE and ROAD_REAL_DISTANCE_UNIT_ID in the table ROAD_CONNECTION)

我需要下一个问题的帮助,这个问题和这个问题很相似。但是,建议的解决方案在我的情况下不起作用。我有标记为@Embbedable的类RoadDistance和标记为@ embbedcloader类RoadMetricLoader,也就是@Embbedable,它包含两个RoadDistante类型的属性。还有一个类RoadConnection,它是一个实体,包含RoadmetricLoader类型的属性。我没有成功地覆盖类RoadMetricLoader的属性(@AttributeOverride)(我没有在table ROAD_CONNECTION中获得ROAD_ESTIMATED_DISTANCE_VALUE、road_estimated_distance_id、ROAD_REAL_DISTANCE_VALUE和ROAD_REAL_DISTANCE_UNIT_ID)

The database is MySQL 5.2.21 and the libraries used for JPA 2.0 are the ones from EclipseLink 2.4.1

数据库是MySQL 5.2.21,用于JPA 2.0的库是来自EclipseLink 2.4.1的库

I have tried different options but none of them work. I show all the options in commented blocks in the code you can see below. When uncommented one option keep the others commented. These are the result I get in each case:

我尝试过不同的选择,但没有一个行得通。我展示了代码中注释块中的所有选项,如下所示。当未注释时,一个选项保留其他的注释。这些是我在每种情况下得到的结果:

OPTION 1: It does not return any error but in the table ROAD_CONNECTION I get only two fieds: VALUE and UNIT_ID.

选项1:它不返回任何错误,但是在表ROAD_CONNECTION中,我只得到两个fieds: VALUE和UNIT_ID。

OPTION 2: The same result as OPTION 1.

选项2:与选项1相同的结果。

OPTION 3: This was my first bet (see the official documentation example 2, and the already indicated link above) but I get the next error

选项3:这是我的第一个赌注(请参阅官方文档示例2,以及上面已经指出的链接),但是我得到了下一个错误

Local Exception Stack: 
Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: sun.misc.Launcher$AppClassLoader@138d107f
Internal Exception: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [jamUnit] failed.
Internal Exception: Exception [EclipseLink-7309] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The attribute named [estimatedRoadDistance.unit] from the embeddable class [class net.question.RoadMetricLoader] is not a valid mapping to use with an attribute override for the attribute [metricLoader] on class [class net.question.RoadConnection].
    at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:127)
    at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:118)
    at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
    at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
    (...)

OPTION 4: The same as OPTION 1 and 2.

选项4:与选项1和2相同。

OPTION 5:

选择5:

Local Exception Stack: 
Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: sun.misc.Launcher$AppClassLoader@12360be0
Internal Exception: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [jamUnit] failed.
Internal Exception: Exception [EclipseLink-7309] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The attribute named [unit] from the embeddable class [class net.question.RoadDistance] is not a valid mapping to use with an attribute override for the attribute [estimatedRoadDistance] on class [class net.question.RoadMetricLoader].
    at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:127)
    at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:118)
    at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
    at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
    (...)

@Entity
@Table(name="UNIT")
public class Unit {

    private Long id;
    private String name;
    private String measureSystemCode;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator="UNIT_SEQ_GENERATOR")
    @SequenceGenerator(name="UNIT_SEQ_GENERATOR", sequenceName="UNIT_SEQ")
    @Column(name = "ID")
    public Long getId() {
        return id;
    }

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

    @Column(name = "NAME", nullable = false)
    public String getName() {
        return name;
    }

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

    @Column(name = "MEASURE_SYSTEM_CODE", nullable = false)
    public String getMeasureSystemCode() {
        return measureSystemCode;
    }

    public void setMeasureSystemCode(String measureSystemCode) {
        this.measureSystemCode = measureSystemCode;
    }

}

@MappedSuperclass
public abstract class Metric<V extends Comparable<V>> {

    private V value;
    private Unit unit;

    @Column(name = "VALUE", nullable = false)
    public V getValue() {
        return value;
    }

    public void setValue(V value) {
        this.value = value;
    }

    @ManyToOne
    @JoinColumn(name = "UNIT_ID", nullable = false)
    public Unit getUnit() {
        return unit;
    }

    public void setUnit(Unit unit) {
        this.unit = unit;
    }

}

@MappedSuperclass
public abstract class ScalarPhysicalMetric<M extends Number & Comparable<M>>
extends Metric<M> {

}

@Embeddable
public final class RoadDistance extends ScalarPhysicalMetric<Double> {

}

/*
// -##----------- OPTION 4 ---------->
@AttributeOverrides({
    @AttributeOverride(name = "estimatedRoadDistance.value", column = @Column(name = "ROAD_ESTIMATED_DISTANCE_VALUE")),
    @AttributeOverride(name = "estimatedRoadDistance.unit", column = @Column(name = "ROAD_ESTIMATED_DISTANCE_UNIT_ID")),
    @AttributeOverride(name = "realRoadDistance.value", column = @Column(name = "ROAD_REAL_DISTANCE_VALUE")),
    @AttributeOverride(name = "realRoadDistance.unit", column = @Column(name = "ROAD_REAL_DISTANCE_UNIT_ID"))
})
// <---------- OPTION 4 -----------##-
*/
@Embeddable
public final class RoadMetricLoader {

    private RoadDistance estimatedRoadDistance;
    private RoadDistance realRoadDistance;

    @Embedded
    /*
    // -##----------- OPTION 5 ---------->
    @AttributeOverrides({
        @AttributeOverride(name = "value", column = @Column(name = "ROAD_ESTIMATED_DISTANCE_VALUE")),
        @AttributeOverride(name = "unit", column = @Column(name = "ROAD_ESTIMATED_DISTANCE_UNIT_ID"))
    })
    // <---------- OPTION 5 -----------##-
    */
    public RoadDistance getEstimatedRoadDistance() {
        return estimatedRoadDistance;
    }

    public void setEstimatedRoadDistance(RoadDistance estimatedRoadDistance) {
        this.estimatedRoadDistance = estimatedRoadDistance;
    }

    @Embedded
    /*
    // -##----------- OPTION 5 ---------->
    @AttributeOverrides({
        @AttributeOverride(name = "value", column = @Column(name = "ROAD_REAL_DISTANCE_VALUE")),
        @AttributeOverride(name = "unit", column = @Column(name = "ROAD_REAL_DISTANCE_UNIT_ID"))
    })
    // <---------- OPTION 5 -----------##-
    */
    public RoadDistance getRealRoadDistance() {
        return realRoadDistance;
    }

    public void setRealRoadDistance(RoadDistance realRoadDistance) {
        this.realRoadDistance = realRoadDistance;
    }

}

// -##----------- OPTION 1 ---------->
/*
@AttributeOverrides({
    @AttributeOverride(name = "estimatedRoadDistance.value", column = @Column(name = "ROAD_ESTIMATED_DISTANCE_VALUE")),
    @AttributeOverride(name = "estimatedRoadDistance.unit", column = @Column(name = "ROAD_ESTIMATED_DISTANCE_UNIT_ID")),
    @AttributeOverride(name = "realRoadDistance.value", column = @Column(name = "ROAD_REAL_DISTANCE_VALUE")),
    @AttributeOverride(name = "realRoadDistance.unit", column = @Column(name = "ROAD_REAL_DISTANCE_UNIT_ID"))
})
// <---------- OPTION 1 -----------##-
*/
/*
// -##----------- OPTION 2 ---------->
@AttributeOverrides({
    @AttributeOverride(name = "metricLoader.estimatedRoadDistance.value", column = @Column(name = "ROAD_ESTIMATED_DISTANCE_VALUE")),
    @AttributeOverride(name = "metricLoader.estimatedRoadDistance.unit", column = @Column(name = "ROAD_ESTIMATED_DISTANCE_UNIT_ID")),
    @AttributeOverride(name = "metricLoader.realRoadDistance.value", column = @Column(name = "ROAD_REAL_DISTANCE_VALUE")),
    @AttributeOverride(name = "metricLoader.realRoadDistance.unit", column = @Column(name = "ROAD_REAL_DISTANCE_UNIT_ID"))
})
// <---------- OPTION 2 -----------##-
*/
@Entity
@Table(name="ROAD_CONNECTION")
public class RoadConnection {

    private Long id;
    private String pointA;
    private String pointB;
    private RoadMetricLoader metricLoader;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator="ROAD_CONNECTION_SEQ_GENERATOR")
    @SequenceGenerator(name="ROAD_CONNECTION_SEQ_GENERATOR", sequenceName="ROAD_CONNECTION_SEQ")
    @Column(name = "ID")
    public Long getId() {
        return id;
    }

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

    @Column(name = "POINT_A", nullable = false)
    public String getPointA() {
        return pointA;
    }

    public void setPointA(String pointA) {
        this.pointA = pointA;
    }

    @Column(name = "POINT_B", nullable = false)
    public String getPointB() {
        return pointB;
    }

    public void setPointB(String pointB) {
        this.pointB = pointB;
    }

    /*
    // -##----------- OPTION 3 ---------->
    @AttributeOverrides({
        @AttributeOverride(name = "estimatedRoadDistance.value", column = @Column(name = "ROAD_ESTIMATED_DISTANCE_VALUE")),
        @AttributeOverride(name = "estimatedRoadDistance.unit", column = @Column(name = "ROAD_ESTIMATED_DISTANCE_UNIT_ID")),
        @AttributeOverride(name = "realRoadDistance.value", column = @Column(name = "ROAD_REAL_DISTANCE_VALUE")),
        @AttributeOverride(name = "realRoadDistance.unit", column = @Column(name = "ROAD_REAL_DISTANCE_UNIT_ID"))
    })
    // <---------- OPTION 3 -----------##-
    */
    @Embedded
    public RoadMetricLoader getMetricLoader() {
        return metricLoader;
    }

    public void setMetricLoader(RoadMetricLoader metricLoader) {
        this.metricLoader = metricLoader;
    }

}

2 个解决方案

#1


4  

AttributeOverride are for basic mappings. What you need is AssociationOverride : http://docs.oracle.com/javaee/6/api/javax/persistence/AssociationOverride.html

AttributeOverride用于基本映射。您需要的是AssociationOverride: http://docs.oracle.com/javaee/6/api/javax/persistence/AssociationOverride.html

#2


1  

As @Chris suggested AssociatedOverride must be used. If you change RoadMetricLoader as follows:

正如@Chris建议必须使用AssociatedOverride。如果你改变道路雷达如下:

public final class RoadMetricLoader {

    private RoadDistance estimatedRoadDistance;
    private RoadDistance realRoadDistance;

    @Embedded
    @AttributeOverrides({
        @AttributeOverride(name = "value", column = @Column(name = "ROAD_ESTIMATED_DISTANCE_VALUE"))
    })
    @AssociationOverrides({  
        @AssociationOverride(name = "unit", joinColumns = @JoinColumn(name = "ROAD_ESTIMATED_DISTANCE_UNIT_ID"))
    })  
    public RoadDistance getEstimatedRoadDistance() {
        return estimatedRoadDistance;
    }

    public void setEstimatedRoadDistance(RoadDistance estimatedRoadDistance) {
        this.estimatedRoadDistance = estimatedRoadDistance;
    }

    @Embedded
    @AttributeOverrides({
        @AttributeOverride(name = "value", column = @Column(name = "ROAD_REAL_DISTANCE_VALUE"))
    })
    @AssociationOverrides({  
        @AssociationOverride(name = "unit", joinColumns = @JoinColumn(name = "ROAD_REAL_DISTANCE_UNIT_ID"))
    })  
    public RoadDistance getRealRoadDistance() {
        return realRoadDistance;
    }

    public void setRealRoadDistance(RoadDistance realRoadDistance) {
        this.realRoadDistance = realRoadDistance;
    }

now the table is created correctly in the DB:

现在,在DB中正确地创建了表:

JPA 2.0: @AttributeOverrides和继承属性之间的关系不太好。

In this case, you can also get rid of @AttributeOverrides and @AssociationOverrides as there is only one element for each one.

在本例中,还可以删除@AttributeOverrides和@AssociationOverrides,因为每个人都只有一个元素。

#1


4  

AttributeOverride are for basic mappings. What you need is AssociationOverride : http://docs.oracle.com/javaee/6/api/javax/persistence/AssociationOverride.html

AttributeOverride用于基本映射。您需要的是AssociationOverride: http://docs.oracle.com/javaee/6/api/javax/persistence/AssociationOverride.html

#2


1  

As @Chris suggested AssociatedOverride must be used. If you change RoadMetricLoader as follows:

正如@Chris建议必须使用AssociatedOverride。如果你改变道路雷达如下:

public final class RoadMetricLoader {

    private RoadDistance estimatedRoadDistance;
    private RoadDistance realRoadDistance;

    @Embedded
    @AttributeOverrides({
        @AttributeOverride(name = "value", column = @Column(name = "ROAD_ESTIMATED_DISTANCE_VALUE"))
    })
    @AssociationOverrides({  
        @AssociationOverride(name = "unit", joinColumns = @JoinColumn(name = "ROAD_ESTIMATED_DISTANCE_UNIT_ID"))
    })  
    public RoadDistance getEstimatedRoadDistance() {
        return estimatedRoadDistance;
    }

    public void setEstimatedRoadDistance(RoadDistance estimatedRoadDistance) {
        this.estimatedRoadDistance = estimatedRoadDistance;
    }

    @Embedded
    @AttributeOverrides({
        @AttributeOverride(name = "value", column = @Column(name = "ROAD_REAL_DISTANCE_VALUE"))
    })
    @AssociationOverrides({  
        @AssociationOverride(name = "unit", joinColumns = @JoinColumn(name = "ROAD_REAL_DISTANCE_UNIT_ID"))
    })  
    public RoadDistance getRealRoadDistance() {
        return realRoadDistance;
    }

    public void setRealRoadDistance(RoadDistance realRoadDistance) {
        this.realRoadDistance = realRoadDistance;
    }

now the table is created correctly in the DB:

现在,在DB中正确地创建了表:

JPA 2.0: @AttributeOverrides和继承属性之间的关系不太好。

In this case, you can also get rid of @AttributeOverrides and @AssociationOverrides as there is only one element for each one.

在本例中,还可以删除@AttributeOverrides和@AssociationOverrides,因为每个人都只有一个元素。