我是 JPA/Hyperjaxb 领域的新手。我的目标是在 Author 和 Book 表(postgres 数据库)之间生成多对多映射。
在数据库中 - Author 表有列 - id、name 和 Book 表有列 - id、title。我有一个联结(链接)表 AUTHORS_BOOKS ,它具有栏aid、bid(其中aid 映射到 Author 表中的 id 字段和bid 映射到 Book 表中的 id 字段)。
我们正在公开一个 web 服务(让我们不要关注为什么要使用 web 服务),以允许客户查询/浏览作者和书籍。对于 web 服务,我正在使用 hyperjaxb 表示法创建 pojos(这就是它的本来面目)。
这是我的 types.xsd 文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<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="title" type="xs:string"/>
<xs:element name="authors" type="tns:author" minOccurs="0" maxOccurs="1000"/>
</xs:sequence>
</xs:complexType>
这是 bindings.xjb 文件:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings schemaLocation="sampletypes.xsd"
node="/xs:schema">
<jaxb:bindings node="xs:complexType[@name='author']">
<hj:entity>
<orm:table name="AUTHORS">
<orm:unique-constraint>
<orm:column-name>NAME</orm:column-name>
</orm:unique-constraint>
</orm:table>
</hj:entity>
</jaxb:bindings>
<jaxb:bindings node="xs:complexType[@name='cocom']">
<hj:entity>
<orm:table name="BOOKS">
<orm:unique-constraint>
<orm:column-name>NAME</orm:column-name>
</orm:unique-constraint>
</orm:table>
</hj:entity>
</jaxb:bindings>
<jaxb:bindings
node="xs:complexType[@name='author']//xs:element[@name='books']">
<hj:many-to-many name="books">
<orm:join-table name="AUTHORS_BOOKS">
<orm:join-column name="aid" referenced-column-name="ID" />
<orm:inverse-join-column name="bid" referenced-column-name="ID" />
</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" mappedBy="books">
<hj:cascade>
<hj:cascade-persist/>
</hj:cascade>
</hj:many-to-many>
</jaxb:bindings>
<jaxb:bindings
node="xs:complexType[@name='author']//xs:element[@name='id']">
<hj:id>
<orm:generated-value strategy="AUTO" />
</hj:id>
</jaxb:bindings>
<jaxb:bindings
node="xs:complexType[@name='book']//xs:element[@name='id']">
<hj:id>
<orm:generated-value strategy="AUTO" />
</hj:id>
</jaxb:bindings>
</jaxb:bindings>
这是生成的 Author.java (部分):
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "author", propOrder = {
"id",
"name",
"books"
})
@Entity(name = "Author")
@Table(name = "AUTHOR", uniqueConstraints = {
@UniqueConstraint(columnNames = {
"NAME"
})
})
@Inheritance(strategy = InheritanceType.JOINED)
public class Author
implements Serializable, Equals, HashCode, ToString
{
//some othe stuff
@ManyToMany(targetEntity = Book.class, cascade = {
CascadeType.ALL
})
@JoinTable(name = "AUTHORS_BOOKS", joinColumns = {
@JoinColumn(name = "aid", referencedColumnName = "ID")
}, inverseJoinColumns = {
@JoinColumn(name = "bid", referencedColumnName = "ID")
})
public List<Book> getBooks() {
if (books == null) {
books = new ArrayList<Book>();
}
return this.books;
}
}
这是生成 Book.java 的(部分):
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "book", propOrder = {
"id",
"title",
"authors"
})
@Entity(name = "Book")
@Table(name = "BOOK", uniqueConstraints = {
@UniqueConstraint(columnNames = {
"NAME"
})
})
@Inheritance(strategy = InheritanceType.JOINED)
public class Book
implements Serializable, Equals, HashCode, ToString
{
private final static long serialVersionUID = 1L;
protected long id;
@XmlElement(required = true)
protected String title;
protected List<Author> authors;
/**
* Gets the value of the id property.
*
*/
@Id
@Column(name = "ID", scale = 0)
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return id;
}
/**
* Sets the value of the id property.
*
*/
public void setId(long value) {
this.id = value;
}
@Basic
@Column(name = "NAME_", length = 255)
public String getName() {
return name;
}
public void setName(String value) {
this.name = value;
}
@ManyToMany(targetEntity = Book.class, cascade = {
CascadeType.ALL
})
@JoinTable(name = "AUTHORS_BOOKS", joinColumns = {
@JoinColumn(name = "PARENT_AUTHOR_ID")
}, inverseJoinColumns = {
@JoinColumn(name = "CHILD_BOOK_ID")
})
public List<Book> getBooks() {
if (books == null) {
books = new ArrayList<Book>();
}
return this.books;
}
我不确定我做错了什么,但是当我将war文件部署到tomcat时出现以下错误(7.0.34,还尝试了其他几个7.0.x版本)
Error Message:
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class:com.mycompany.Author.books[com.mycompany.Book]
我一直在网上搜索答案,要么没有解决方案,要么大多数错误是由于 @ManyToMany 标记中缺少 @Entity 或 @Id 或 targetEntity 字段。我真的可以在这方面使用一些指针!非常感谢您的时间和帮助!谢谢。