我是JPA / Hyperjaxb竞技场的新手。我的目标是在Author和Book表(postgres数据库)之间生成多对多映射。
在数据库中 - 作者表有列--id,name和Book表有列--id,title。 我有一个联结(链接)表AUTHORS_BOOKS,它有列辅助,bid(其中援助映射到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字段。我真的可以用这个指针!非常感谢您的时间和帮助!感谢。
答案 0 :(得分:8)
谢谢大家的帮助!原来这是一个映射问题。在/src/main/resources/persistence.xml文件中,我忘了添加完整的类路径(对于Book.java),因此Hibernate无法找到它。所以这更像是一个集成问题。再次感谢您的帮助!
答案 1 :(得分:0)
问题是,为什么Hibernate认为com.mycompany.Book未被映射。
我怀疑重复类或类似的问题。首先尝试运行一个孤立的往返测试(HJ3提供了一些支持)。这显然与Tomcat版本等无关。
答案 2 :(得分:0)
也一样。 TUrns我愚蠢到在oneToMany类而不是List中编写List ...