私は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表記を使用してpojoを作成しています(これもその通りです)。
これが私の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]
Webで回答を検索しましたが、解決策がないか、ほとんどのエラーが@ManyToManyタグの@Entityまたは@IdまたはtargetEntityフィールドの欠如によるものでした。私は本当にこれにいくつかのポインタを使うことができました!本当にあなたの時間と助けに感謝します!ありがとう。