gpt4 book ai didi

java - 我是否可以使用多对多关系在另一个实体上拥有实体集合?

转载 作者:太空宇宙 更新时间:2023-11-04 14:09:38 25 4
gpt4 key购买 nike

我在这里搜索了几篇文章,以及 Eclipse wiki 中的关系映射,但我仍然收到以下错误:

Exception Description: [class package.Class1] uses a non-entity [class package.Class2] as target entity in the relationship attribute [field list1].

我尝试过的:

@ManyToMany, @OneToMany, @ManyToOne, @OneToOne
Collection, List, ArrayList

第一类:

@Entity
@Table(name = "Table_1")
public class Class1 {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "IdGen")
@Column(name = "ID")
private int _id;

@SequenceGenerator(
name = "IdGen",
sequenceName = "INC_ID",
allocationSize = 1)

@Column(name = "Table_1_TYPE")
private int _typeId;

@ManyToMany
@JoinTable(name = "Table_1_TO_Table_2",
joinColumns= @JoinColumn(name = "ID"),
inverseJoinColumns= @JoinColumn(name = "CID"))
private List<Class2> list1;

2级:

@Entity
@Table(name = "Table_2")
public class Class2 {

@Id
@ManyToMany
@JoinTable(name = "Table_1_TO_Table_2",
joinColumns= @JoinColumn(name = "CID"),
inverseJoinColumns = @JoinColumn(name = "ID"))
private int _id;

@Column(name = "Value")
private String _value;

我的情况是,Table_2 中的多个“CID”可以属于 Table_1 中的单个“ID”。我的第三个表(其关系表)“Table_1_TO_Table_2”有两列:ID 和 CID。

编辑 2/13 - 9:30 :我还检查了我的持久性文件,两个类都已添加到其中。但仍然出现Class2不被识别为实体的问题。

最佳答案

这个程序有多个错误,我会一一解释

  1. 非实体类

Exception Description: [class package.Class1] uses a non-entity [class package.Class2] as target entity in the relationship attribute [field list1].

解决方案:在hibernate.cfg.xml中声明Class2。喜欢

                <mapping class="com.test.dto.Class2"/>
  • 在 Class1 上生成 list1 的 getter/setter,以便您可以实例化 class1 对象并在此列表中设置 class2 的对象

  • Exception in thread "main" org.hibernate.AnnotationException: Unknown
    Id.generator: IdGen
    at org.hibernate.cfg.BinderHelper.makeIdGenerator(BinderHelper.java:638)

    解决方案-->使用它将序列生成器移动到字段上方

              public class Class1 {
    @SequenceGenerator(name = "IdGen",sequenceName = "INC_ID",
    allocationSize = 1)

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE,
    generator="IdGen")

    @Column(name = "ID")
    private int _id;
  • Exception in thread "main" org.hibernate.AnnotationException: Illegal >attempt to map a non collection as a @OneToMany, @ManyToMany or >@CollectionOfElements: com.test.dto.Class2._id at >org.hibernate.cfg.annotations.CollectionBinder.getCollectionBinder(CollectionBi>nder.java:327) at >org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.j>ava:1719) at >org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBi>nder.java:766)

  • 在这里,您尝试实现 oneToMany 映射,应该如下完成

        @Table(name = "Table_1")
    public class Class1 {
    ....
    @OneToMany(mappedBy="cid")
    private List<Class2> list1;

    @Table(name = "Table_2")
    public class Class2 {
    ...
    @ManyToOne
    @JoinColumn(name="cid")
    private Class1 cid;
  • RootClass:289 - HHH000039: Composite-id class does not override hashCode(): >com.test.dto.Class2 Exception in thread "main" org.hibernate.MappingException: Composite-id class >must implement Serializable: com.test.dto.Class2

  • 解决方案要么使用可序列化接口(interface)实现 Class2,要么

    在Class2中定义主键

          @Entity
    @Table(name = "Table_2")
    public class Class2 {
    @Id
    @GeneratedValue
    private int class2Id;

    关于java - 我是否可以使用多对多关系在另一个实体上拥有实体集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28490847/

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