gpt4 book ai didi

JPA 2.0 : @AttributeOverrides and inherited attributes don't get on well with each other

转载 作者:行者123 更新时间:2023-12-04 15:01:10 24 4
gpt4 key购买 nike

我需要帮助解决下一个与此非常相似的问题 one .但是,建议的解决方案在我的情况下不起作用。我有标记为@Embbedable 的类 RoadDistance 和类 RoadMetricLoader,也就是 @Embbedable 并且包含 RoadDistance 类型的两个属性。还有一个 RoadConnection 类,它是一个实体并且包含一个 RoadmetricLoader 类型的属性。我没有成功覆盖类 RoadDistance 的 RoadMetricLoader 的属性(@AttributeOverride)(我没有得到 ROAD_ESTIMATED_DISTANCE_VALUE、ROAD_ESTIMATED_DISTANCE_UNIT_ID、ROAD_REAL_DISTANCE_VALUE 和 ROAD_REAL_DISTANCE_UNITCON_ID_ID 表中的字段)

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

我尝试了不同的选择,但没有一个有效。我在代码中的注释块中显示了所有选项,您可以在下面看到。当取消注释一个选项时,保留其他选项。这些是我在每种情况下得到的结果:

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

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

选项 3 :这是我的第一个赌注(参见 official documentation example 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)
(...)

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

选项 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;
}

}

最佳答案

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

关于JPA 2.0 : @AttributeOverrides and inherited attributes don't get on well with each other,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14420327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com