如何映射Java对象与Java对象映射之间的关系

时间:2018-09-12 14:38:07

标签: java hibernate hashmap

我有一个名为

的Java对象。
using (StreamReader reader = new StreamReader(await selectedFile.OpenStreamForReadAsync()))
            {
                while ((nextLine = await reader.ReadLineAsync()) != null)
                {
                    contents.AppendFormat("{0}. ", lineCounter);
                    contents.Append(nextLine);
                    contents.AppendLine();
                    lineCounter++;
                    if (lineCounter > 3)
                    {
                        contents.AppendLine("Only first 3 lines shown.");
                        break;
                    }
                }
            }

这有可能使您休眠吗?另一个对象是实体吗? 映射应该在MyObject类中是什么?

阅读后,我对AnotherObject进行了如下注释

public class MyObject {

@ElementCollection 
Map<Integer, AnotherJavaObject> customMap;
}

public class AnotherObject {

String value;
int value;
}

但这给我一个错误

@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

1 个答案:

答案 0 :(得分:0)

尝试一下

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;
}