I am newbie to JPA/Hyperjaxb arena. My goal is to generate a many-to-many mapping between Author and Book tables (postgres database).
In the database- Author table has columns - id, name and Book table has columns - id, title. I have a junction (linking) table AUTHORS_BOOKS that has columns aid, bid (where aid maps to id field in the Author table and bid maps to id field in the Book table).
We are exposing a webservice (lets not focus on why a webservice) to allow clients to query/crud on Authors and Books. For the webservice I am creating pojos using hyperjaxb notations (again thats just how its been).
Here is my types.xsd file:
<?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>
Here is the bindings.xjb file:
<?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>
Here is (part of) the generated 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;
}
}
Here is (part of) the generate 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;
}
I am not sure what I am doing wrong but I get the following error when I deploy the war file to tomcat (7.0.34, also tried couple other 7.0.x versions)
Error Message:
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class:com.mycompany.Author.books[com.mycompany.Book]
I been searching the web for answers and either there are no solutions or most errors were due to lack of @Entity or @Id or targetEntity field in @ManyToMany tag. I could really use some pointers on this! Really appreciate your time and help! Thanks.
Thank you all for help! Turns out it was a mapping problem. In the /src/main/resources/persistence.xml file I forgot to add the full class path (for Book.java) so Hibernate couldn't find it. So this was more of an integration issue. Thank you again for help!