我是JPA/Hyperjaxb arena的新手。我的目标是在Author和Book表(postgres数据库)之间生成多对多映射。
在数据库中,Author表包含columns - id、name,Book表包含columns - id、title。我有一个连接(链接)表AUTHORS_BOOKS,它有列aid、bid (其中aid映射到Author表中的id字段,bid映射到Book表中的id字段)。
我们公开了一个webservice (让我们不要关注为什么是webservice),以允许客户端查询/crud作者和图书。对于webservice,我使用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字段造成的。在这方面我真的需要一些指点!非常感谢您的时间和帮助!谢谢。
发布于 2013-01-16 03:29:42
感谢大家的帮助!事实证明这是一个映射问题。在/src/main/resources/persistence.xml文件中,我忘记添加完整的类路径(对于Book.java),所以Hibernate找不到它。因此,这更多的是一个集成问题。再次感谢您的帮助!
发布于 2013-01-14 01:12:31
问题是,为什么Hibernate认为com.mycompany.Book是未映射的。
我怀疑有重复类或类似的问题。首先尝试运行隔离往返测试(HJ3对此提供了一些支持)。这显然与Tomcat版本等无关。
发布于 2013-06-21 01:00:46
都是一样的。List out我愚蠢到在oneToMany类中编写TUrns而不是List...
https://stackoverflow.com/questions/14288439
复制相似问题