Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
hibernate org.hibernate.Mapping.Table$ForeignKeyKey.equals中的多对多映射(Table.java:95)_Java_Hibernate_Orm - Fatal编程技术网

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

hibernate org.hibernate.Mapping.Table$ForeignKeyKey.equals中的多对多映射(Table.java:95),java,hibernate,orm,Java,Hibernate,Orm,请帮助我理解xml中的错误,因为它在构建会话工厂时表示错误 XML: <!-- Many to Many starts --> <class name="Bishop" table="BISHOP"> <id name="id" column="BISHOP_ID"> <generator class="native" /> </id> <property name="name" colum

请帮助我理解xml中的错误,因为它在构建会话工厂时表示错误

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();
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 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 -->