首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >定义嵌入式属性的获取类型的JPA SubGraph

定义嵌入式属性的获取类型的JPA SubGraph
EN

Stack Overflow用户
提问于 2014-12-18 17:34:42
回答 1查看 3.8K关注 0票数 11

我有一个实体骑行,嵌入了一个可嵌入的“实体”路径。路由有一个具有ManyToMany关系的列表属性城镇,因此它具有获取类型懒惰(我不想使用want )。因此,我想为实体Ride定义一个NamedEntityGraph,用带有实例化城镇列表的路由加载Ride对象。但是当我部署我的战争时,我得到了这样的例外:

java.lang.IllegalArgumentException:属性路由不是托管类型

Ride

代码语言:javascript
复制
@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
}

路由

代码语言:javascript
复制
@Embeddable
public class Route implements Serializable {
    private static final long serialVersionUID = 1L;

    @ManyToMany
    private List<Town> towns;

    // some getter and setter
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-18 21:32:00

通过查看Hibernate的org.hibernate.jpa.graph.internal.AttributeNodeImpl实现,我们得出结论:@NamedAttributeNode不可能是:

  • 简单类型(Java原语及其包装器、字符串、枚举、时间性、.)
  • 嵌入(用@Embedded注释)
  • 元素集合(用@ElementCollection注释)
代码语言:javascript
复制
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私有列表游程;@可嵌入的私有路由;.}

当然,实体图可能需要相应的更改。

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27552445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档