gpt4 book ai didi

java - hibernate中的多对多映射 org.hibernate.mapping.Table$ForeignKeyKey.equals(Table.java :95)

转载 作者:行者123 更新时间:2023-12-01 12:45:45 24 4
gpt4 key购买 nike

请帮助我理解 xml 中的错误,因为它在构建 session 工厂本身时显示错误。

XML:

<!-- Many to Many starts -->
<class name="Bishop" table="BISHOP">
<id name="id" column="BISHOP_ID">
<generator class="native" />
</id>
<property name="name" column="name" />
<set name="churches" table="BISHOP_CHURCH" inverse="true" cascade="all">
<key column="CHURCH_ID" />
<many-to-many column="BISHOP_ID" />
</set>
</class>
<class name="Church" table="CHURCH">
<id name="id" column="CHURCH_ID">
<generator class="native" />
</id>
<property name="name" column="name" />
<set name="bishops" table="BISHOP_CHURCH" inverse="true">
<key column="CHURCH_ID" />
<many-to-many column="BISHOP_ID" />
</set>
</class>
<!-- Many to Many ends -->

主类:

SessionFactory sessionFactory = new Configuration()
.configure("hibernate.cfg.xml")
.buildSessionFactory(); // this is 23 line error comes.

Bishop b1 = new Bishop();
b1.setName("bishop1");
Bishop b2 = new Bishop();
b2.setName("bishop2");

Set<Bishop> bishops = new HashSet<>();
bishops.add(b1);
bishops.add(b2);

Church c1 = new Church();
c1.setName("church1");
Church c2 = new Church();
c2.setName("church2");

c1.setBishops(bishops);
c2.setBishops(bishops);


Set<Church> churches = new HashSet<>();
churches.add(c1);
churches.add(c2);

b1.setChurches(churches);
b2.setChurches(churches);

Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(b1);
session.save(b2);
session.getTransaction().commit();
session.close();

POJO:

public class Bishop {
private int id;
private String name;
private Set<Church> churches;
//getters & setters
}
public class Church {
private int id;
private String name;
private Set<Bishop> bishops;
//getters & setters
}

异常(exception):

Exception in thread "main" java.lang.NullPointerException
at org.hibernate.mapping.Table$ForeignKeyKey.equals(Table.java:95)
at java.util.HashMap.getEntry(HashMap.java:448)
at java.util.LinkedHashMap.get(LinkedHashMap.java:301)
at org.hibernate.mapping.Table.createForeignKey(Table.java:658)
at org.hibernate.mapping.Table.createForeignKey(Table.java:651)
at org.hibernate.mapping.SimpleValue.createForeignKeyOfEntity(SimpleValue.java:147)
at org.hibernate.mapping.ManyToOne.createForeignKey(ManyToOne.java:61)
at org.hibernate.mapping.Collection.createForeignKeys(Collection.java:419)
at org.hibernate.mapping.Collection.createAllKeys(Collection.java:428)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:71)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1695)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
at sample.pack.TestMany2ManyXML.main(TestMany2ManyXML.java:23)

最佳答案

您可以尝试使用下面的 xml 吗?

<!-- Many to Many starts -->
<class name="Bishop" table="BISHOP">
<id name="id" column="BISHOP_ID">
<generator class="native" />
</id>
<property name="name" column="name" />
<set name="churches" table="BISHOP_CHURCH" inverse="false" lazy="true" fetch="select" cascade="all">

<key>
<column name="CHURCH_ID" not-null="true" />
</key>

<many-to-many entity-name="com.*.*.Church">
<column name="BISHOP_ID" not-null="true" />
</many-to-many>

</set>
</class>

<class name="Church" table="CHURCH">
<id name="id" column="CHURCH_ID">
<generator class="native" />
</id>
<property name="name" column="name" />
<set name="bishops" table="BISHOP_CHURCH" inverse="true" lazy="true" fetch="select">
<key>
<column name="CHURCH_ID" not-null="true" />
</key>
<many-to-many entity-name="com.*.*.Bishop">
<column name="BISHOP_ID" not-null="true" />
</many-to-many>

</set>
</class>
<!-- Many to Many ends -->

关于java - hibernate中的多对多映射 org.hibernate.mapping.Table$ForeignKeyKey.equals(Table.java :95),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24724885/

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