Я хотел создать отношения «многие ко многим» с ключом композиции через hibernate.Я использовал 3 класса, включая book
, orders
и orderDetail
, за исключением composition key class
.В базе данных есть 3 таблицы с именами BOOK
, ORDERS
и ORDER_DETAIL
.
. Я определил все классы отображения в hibernate.cfg.xml
.
<mapping class="com.bookstore.entity.Book"/>
<mapping class="com.bookstore.entity.OrderDetail"/>
<mapping class="com.bookstore.entity.OrderDetailId"/>
<mapping class="com.bookstore.entity.Orders"/>
<!-- Drop and re-create the database schema on start-up -->
<property name="hibernate.hbm2ddl.auto">update</property>
Вотмои классы показаны ниже.
Книга класса
@Entity
@Table(name="BOOK",catalog = "JSPPROJECTDATABASE")
public class Book implements Serializable{
@Id
@SequenceGenerator(name="BOOK_SEQ", sequenceName="BOOK_SEQ", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="BOOK_SEQ")
@Column(name="BOOK_ID", nullable = false)
private int id;
@OneToMany(mappedBy = "pk.books", cascade=CascadeType.ALL)
private Set<OrderDetail> orderDetails = new HashSet<OrderDetail>();
// constructor
// getter and setter
public Set<OrderDetail> getOrderDetails() {
return orderDetails;
}
public void setOrderDetails(Set<OrderDetail> orderDetails) {
this.orderDetails = orderDetails;
}
}
Для класса заказов
@Entity
@Table(name="ORDERS",catalog = "JSPPROJECTDATABASE")
public class Orders implements Serializable{
@Id
@SequenceGenerator(name="ORDERS_SEQ", sequenceName="ORDERS_SEQ", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ORDERS_SEQ")
@Column(name="ORDER_ID", nullable = false)
private int id;
@OneToMany(mappedBy = "pk.order", cascade=CascadeType.ALL)
private Set<OrderDetail> orderDetails = new HashSet<OrderDetail>();
// constructor
// getter and setter
public Set<OrderDetail> getOrderDetails() {
return orderDetails;
}
public void setOrderDetails(Set<OrderDetail> orderDetails) {
this.orderDetails = orderDetails;
}
}
Класс OrderDetail
@Entity
@Table(name = "ORDER_DETAIL", catalog = "JSPPROJECTDATABASE")
@AssociationOverrides({
@AssociationOverride(name = "pk.order",
joinColumns = @JoinColumn(name = "ORDER_ID")),
@AssociationOverride(name = "pk.books",
joinColumns = @JoinColumn(name = "BOOK_ID")) })
public class OrderDetail implements Serializable{
private OrderDetailId pk = new OrderDetailId();
@Column(name="QUANTITY")
private int quantity;
@Column(name="SUBTOTAL")
private float subTotal;
public OrderDetail() {
super();
// TODO Auto-generated constructor stub
}
@EmbeddedId
public OrderDetailId getPk() {
return pk;
}
public void setPk(OrderDetailId pk) {
this.pk = pk;
}
@Transient
public Book getBook() {
return getPk().getBooks();
}
public void setBook(Book book) {
getPk().setBooks(book);
}
@Transient
public Orders getOrders() {
return getPk().getOrder();
}
public void setOrders(Orders orders) {
getPk().setOrder(orders);
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public float getSubTotal() {
return subTotal;
}
public void setSubTotal(float subTotal) {
this.subTotal = subTotal;
}
}
Composititon Class, названный для OrderDetailId
@Embeddable
public class OrderDetailId implements Serializable{
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "BOOK_ID",insertable = false, updatable = false,nullable = false)
private Book books;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "ORDER_ID",insertable = false, updatable = false,nullable = false)
private Orders order;
public Book getBooks() {
return books;
}
public void setBooks(Book books) {
this.books = books;
}
public Orders getOrder() {
return order;
}
public void setOrder(Orders order) {
this.order = order;
}
}
Когда я запускаю код, на экране появляется ошибка.
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table REVIEW add constraint FKf8kovbt4otqcvp75w008agrbx foreign key (CUSTOMER_ID) references CUSTOMER" via JDBC Statement
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table REVIEW add constraint FKf339wkg63wmuutk10j4iowly0 foreign key (BOOK_ID) references BOOK" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table ORDERS add constraint FKkdbly1ij6f4kqh378kfne6ilx foreign key (CUSTOMER_ID) references CUSTOMER" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table ORDER_DETAIL add constraint FKag30vapylo13u3lax0elv3n06 foreign key (order) references ORDERS" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table ORDER_DETAIL add constraint FKqk2p0pl26ysleeejh8isy44or foreign key (books) references BOOK" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table BOOK add constraint FKprh5cdnlwefrxo30ausnijl3d foreign key (CATEGORY_ID) references CATEGORY" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table ORDER_DETAIL add order raw(255) not null" via JDBC Statement
Если я определил какую-либо неправильную переменную в классе илилюбая неправильная связь в его отношениях, как я могу это исправить?