我正在尝试使用 jaxb 自动生成一些包含作者和书籍之间多对多关系的休眠映射类。数据库是postgres。我想要实现的是 Authors.java 和 Books.java,如下所示:
目标:Books.java
@ManyToMany(targetEntity = Author.class, cascade = {
CascadeType.ALL
}, mappedBy="books")
public List<Author> getAuthors() {
if (authors == null) {
authors = new ArrayList<AUTHOR>();
}
return this.authors;
}
这是 bindings.xjb(它的一部分)
<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb" version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:hj="http://hyperjaxb3.jvnet.org/ejb/schemas/customizations"
xmlns:orm="http://java.sun.com/xml/ns/persistence/orm"
jaxb:extensionBindingPrefixes="hj orm">
<jaxb:bindings schemaLocation="types.xsd"
node="/xs:schema">
<jaxb:bindings
node="xs:complexType[@name='author']//xs:element[@name='books']">
<hj:many-to-many>
<orm:join-table name="AUTHORS_BOOKS">
<orm:join-column name="aid" />
<orm:inverse-join-column name="bid"/>
</orm:join-table>
</hj:many-to-many>
</jaxb:bindings>
<jaxb:bindings
node="xs:complexType[@name='book']//xs:element[@name='authors']">
<hj:many-to-many name="authors" mapped-by="books">
<hj:cascade>
<hj:cascade-persist/>
</hj:cascade>
</hj:many-to-many>
</jaxb:bindings>
<jaxb:bindings
node="xs:complexType[@name='author']//xs:element[@name='aid']">
<hj:id>
<orm:generated-value strategy="AUTO" />
</hj:id>
</jaxb:bindings>
<jaxb:bindings
node="xs:complexType[@name='book']//xs:element[@name='bid']">
<hj:id>
<orm:generated-value strategy="AUTO" />
</hj:id>
</jaxb:bindings>
</bindings>
这是 types.xsd(它的一部分)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" targetNamespace="http://catapult.jieddo.mil/feeds/domain"
xmlns:tns="http://catapult.jieddo.mil/feeds/domain"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="author">
<xs:sequence>
<xs:element name="id" type="xs:long"/>
<xs:element name="name" type="xs:string" />
<xs:element name="books" type="tns:book" minOccurs="0" maxOccurs="1000"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="book">
<xs:sequence>
<xs:element name="id" type="xs:long"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="authors" type="tns:author" minOccurs="0" maxOccurs="1000"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
使用上述绑定和 xsd,我生成的文件如下所示。
当前:Authors.java
@ManyToMany(targetEntity = Book.class, cascade = {
CascadeType.ALL
})
@JoinTable(name = "AUTHORS_BOOKS", joinColumns = {
@JoinColumn(name = "aid")
}, inverseJoinColumns = {
@JoinColumn(name = "bid")
})
public List<Book> getBooks() {
if (books == null) {
books = new ArrayList<Book>();
}
return this.books;
}
当前: Books.java
@ManyToMany(targetEntity = Author.class, cascade = {
CascadeType.ALL
}, mappedBy="books")
@JoinTable(name = "BOOK_AUTHORS_BOOK", joinColumns = {
@JoinColumn(name = "PARENT_BOOK_ID")
}, inverseJoinColumns = {
@JoinColumn(name = "CHILD_AUTHOR_ID")
})
public List<Author> getAuthors() {
if (authors == null) {
authors = new ArrayList<AUTHOR>();
}
return this.authors;
}
在当前的 Book.java 中,我认为不应该存在 @JoinTable 表示法(表名也是错误的)。如果我使获取延迟或在 bindings.xjb 中未声明它,我会一直遇到延迟初始化错误
org.hibernate.LazyInitializationException:未能延迟初始化角色集合
如果我进行 fetch="EAGER" 那么我收到以下错误:
org.hibernate.loader.MultipleBagFetchException:不能同时获取多个包
而且我认为我遇到了这些问题,因为我的映射中的某些内容很愚蠢。关于这里发生了什么的任何想法?