我有一个实体骑行,嵌入了一个可嵌入的“实体”路径。路由有一个具有ManyToMany关系的列表属性城镇,因此它具有获取类型懒惰(我不想使用want )。因此,我想为实体Ride定义一个NamedEntityGraph,用带有实例化城镇列表的路由加载Ride对象。但是当我部署我的战争时,我得到了这样的例外:
java.lang.IllegalArgumentException:属性路由不是托管类型
Ride
@Entity
@NamedQueries({
@NamedQuery(name = "Ride.findAll", query = "SELECT m FROM Ride m")})
@NamedEntityGraphs({
@NamedEntityGraph(
name = "rideWithInstanciatedRoute",
attributeNodes = {
@NamedAttributeNode(value = "route", subgraph = "routeWithTowns")
},
subgraphs = {
@NamedSubgraph(
name = "routeWithTowns",
attributeNodes = {
@NamedAttributeNode("towns")
}
)
}
)
})
public class Ride implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Embedded
private Route route;
// some getter and setter
}路由
@Embeddable
public class Route implements Serializable {
private static final long serialVersionUID = 1L;
@ManyToMany
private List<Town> towns;
// some getter and setter
}发布于 2014-12-18 21:32:00
通过查看Hibernate的org.hibernate.jpa.graph.internal.AttributeNodeImpl实现,我们得出结论:@NamedAttributeNode不可能是:
@Embedded注释)@ElementCollection注释)if (attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.BASIC ||
attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.EMBEDDED ) {
throw new IllegalArgumentException(
String.format("Attribute [%s] is not of managed type", getAttributeName())
);
}我在JPA2.1规范中没有发现类似的限制,因此它可能是Hibernate的缺点。
在您的特殊情况下,问题是@NamedEntityGraph引用了可嵌入的Route类,因此Hibernate似乎禁止在实体图中使用它(不幸)。
为了使其正常工作,您需要稍微更改实体模型。我想到了几个例子:
Route定义为实体Route并将其towns字段移动到Ride实体中(简化实体模型)route字段从Ride移动到Town实体,将routedTowns映射添加到Ride实体:
@Entity public class Ride实现了可序列化的{. @ManyToMany(mappedBy = "rides")私有Map routedTowns;.} @Entity公共类城镇实现了可序列化的{. @ManyToMany私有列表游程;@可嵌入的私有路由;.}当然,实体图可能需要相应的更改。
https://stackoverflow.com/questions/27552445
复制相似问题