【问题标题】:Hibernate - Use of @OneToMany or @ManyToMany targeting an unmapped class: com.podro.model.Journey.roadWay[com.podro.model.RoadElement]Hibernate - 使用@OneToMany 或@ManyToMany 定位未映射的类:com.podro.model.Journey.roadWay[com.podro.model.RoadElement]
【发布时间】:2016-08-25 15:31:25
【问题描述】:

我看到了类似的问题,但答案没有帮助。所以,我得到这个错误:

Use of @OneToMany or @ManyToMany targeting an unmapped class: com.podro.model.Journey.roadWay[com.podro.model.RoadElement]

我正在尝试使用 RoadElements 的对象创建列表(它是类 Point 和 Section 的接口)。还有其他方法吗?据我所知,我想这是为此类创建正确映射并拥有此元素列表的唯一方法。

@Entity
@Table(name="Journey")
public class Journey {
    // Some other fields
    @Column(name="road_way")
    @ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
    private List<RoadElement> roadWay;
}


@MappedSuperclass
public interface RoadElement {}

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name="Point")
public class Point implements RoadElement{

    @Id
    @Column(name="id")
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private int id;

    private String name;
    @Column(name="time_in_days")
    private int timeInDays;
    private Rate rating;
}

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name="Section")
public class Section implements RoadElement{

    @Id
    @Column(name="id")
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private int id;

    @Column(name="section_name" , length=100)
    private String sectionName;

    @Column(name="time_in_days")
    private int timeInDays;

    @Column(name="kind_of_transport")
    private Locomotion kindOfTransport;

}

感谢您的回答,我将非常感谢您的帮助!

【问题讨论】:

    标签: java mysql hibernate spring-mvc


    【解决方案1】:

    关联在实体之间。 RoadElement 不是实体。是一个界面。

    你可能不会做你想做的事。 Hibernate 需要知道 roadWay 中包含的实体的类型。

    因此,RoadElement 应该是一个使用@Entity 注释的类,具有唯一标识所有道路元素(路段、点等)中的 RoadElement 的 ID

    Section 和 Point 应该从 RoadElement 扩展,并且不应该有自己的 ID,因为它是从 RoadElement 继承的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 1970-01-01
      相关资源
      最近更新 更多