代码之家  ›  专栏  ›  技术社区  ›  user_mda

如何映射java对象和java对象映射之间的关系

  •  0
  • user_mda  · 技术社区  · 6 年前

    我有一个名为

    public class MyObject {
    
    @ElementCollection 
    Map<Integer, AnotherJavaObject> customMap;
    }
    
    public class AnotherObject {
    
    String value;
    int value;
    }
    

    这有可能和冬眠一起痛吗?另一个对象是实体吗? 类MyObject中的映射应该是什么?

    读完后,我给另一个物体作了如下注解

    @Entity(name = "AnotherObject")
    public class AnotherObject {
    
    @ManyToOne
    Myobject myObject;
    }
    
    And in the MyOBject class created a @OneToMany mappng as
    
    @OneToMany(mappedBy="myObject)
    AnotherObject anotherObject;
    

    但这让我犯了个错误

    org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: MyObject.anotherObject
    

    所以我通过注册类克服了这个错误,但是我在合并时遇到了这个错误

    object references an unsaved transient instance - save the transient instance before flushing:
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   niore    6 年前

    试试这个

    public class MyObject {
    
        int id;
    
        @ManyToMany
        @JoinTable(
            name="AuthorBookGroup",
            joinColumns={@JoinColumn(name="fk_myobject", referencedColumnName="id")},
            inverseJoinColumns={@JoinColumn(name="fk_anotherobject", referencedColumnName="id")})
        @MapKey(name = "value2")
        Map<Integer, AnotherJavaObject> customMap;
    }
    
    public class AnotherObject {
    
        String value1;
        int value2;
        int id;
    }