Hibernate Foreign Key Problems Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!What is the “N+1 selects problem” in ORM (Object-Relational Mapping)?Sort a Map<Key, Value> by valuesWhat are the possible values of the Hibernate hbm2ddl.auto configuration and what do they doWith Eclipselink/JPA, can I have a Foreign Composite Key that shares a field with a Primary Composite Key?Wrong ordering in generated table in jpaHibernate show real SQLHibernate cannot simultaneously fetch multiple bagsHibernate update table B after table A is updatedWhat's the difference between JPA and Hibernate?Hibernate : Why FetchType.LAZY-annotated collection property eagerly loading?
Justification for leaving new position after a short time
What do you call the part of a novel that is not dialog?
PIC mathematical operations weird problem
What is the least dense liquid under normal conditions?
Did the Roman Empire have penal colonies?
My admission is revoked after accepting the admission offer
Israeli soda type drink
Are all CP/M-80 implementations binary compatible?
Is this homebrew racial feat, Stonehide, balanced?
Multiple options vs single option UI
How do I check if a string is entirely made of the same substring?
Seek and ye shall find
What is the term for a person whose job is to place products on shelves in stores?
What was Apollo 13's "Little Jolt" after MECO?
Split coins into combinations of different denominations
How can I wire a 9-position switch so that each position turns on one more LED than the one before?
Is a 5 watt UHF/VHF handheld considered QRP?
Is Electric Central Heating worth it if using Solar Panels?
Multiple fireplaces in an apartment building?
Is Bran literally the world's memory?
Could Neutrino technically as side-effect, incentivize centralization of the bitcoin network?
Is there any hidden 'W' sound after 'comment' in : Comment est-elle?
Do I need to protect SFP ports and optics from dust/contaminants? If so, how?
"Rubric" as meaning "signature" or "personal mark" -- is this accepted usage?
Hibernate Foreign Key Problems
Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar Manara
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!What is the “N+1 selects problem” in ORM (Object-Relational Mapping)?Sort a Map<Key, Value> by valuesWhat are the possible values of the Hibernate hbm2ddl.auto configuration and what do they doWith Eclipselink/JPA, can I have a Foreign Composite Key that shares a field with a Primary Composite Key?Wrong ordering in generated table in jpaHibernate show real SQLHibernate cannot simultaneously fetch multiple bagsHibernate update table B after table A is updatedWhat's the difference between JPA and Hibernate?Hibernate : Why FetchType.LAZY-annotated collection property eagerly loading?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I wanted to design many-to-many relationship with composition key via hibernate.
I used 3 classes including book
,orders
and orderDetail
class except for composition key class
. In the database, there are 3 tables named for BOOK
, ORDERS
and ORDER_DETAIL
.
I defined all mapping class in the 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>
Here are my classes shown below.
Book class
@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;
For Orders class
@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 Class
@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 named for 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;
When I run the code, there is an error appeared on the screen.
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
If I defined any wrong variable in the class or any wrong association in its relationship, how can I fix it?
java hibernate jpa orm hibernate-mapping
add a comment |
I wanted to design many-to-many relationship with composition key via hibernate.
I used 3 classes including book
,orders
and orderDetail
class except for composition key class
. In the database, there are 3 tables named for BOOK
, ORDERS
and ORDER_DETAIL
.
I defined all mapping class in the 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>
Here are my classes shown below.
Book class
@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;
For Orders class
@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 Class
@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 named for 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;
When I run the code, there is an error appeared on the screen.
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
If I defined any wrong variable in the class or any wrong association in its relationship, how can I fix it?
java hibernate jpa orm hibernate-mapping
The property is namedpk.books
, notpk.BOOK
. Andbooks
should really be rebamed tobook
, since it references a single book.
– JB Nizet
Mar 22 at 9:17
Why pk.books is used is defined in the books in the composition id class ,isn't it? If it is, It is similar to define pk.order because of defining order in the compostion id class. I can try it. When I have an issue , I inform you about it.
– kevinBrand
Mar 22 at 9:56
add a comment |
I wanted to design many-to-many relationship with composition key via hibernate.
I used 3 classes including book
,orders
and orderDetail
class except for composition key class
. In the database, there are 3 tables named for BOOK
, ORDERS
and ORDER_DETAIL
.
I defined all mapping class in the 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>
Here are my classes shown below.
Book class
@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;
For Orders class
@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 Class
@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 named for 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;
When I run the code, there is an error appeared on the screen.
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
If I defined any wrong variable in the class or any wrong association in its relationship, how can I fix it?
java hibernate jpa orm hibernate-mapping
I wanted to design many-to-many relationship with composition key via hibernate.
I used 3 classes including book
,orders
and orderDetail
class except for composition key class
. In the database, there are 3 tables named for BOOK
, ORDERS
and ORDER_DETAIL
.
I defined all mapping class in the 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>
Here are my classes shown below.
Book class
@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;
For Orders class
@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 Class
@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 named for 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;
When I run the code, there is an error appeared on the screen.
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
If I defined any wrong variable in the class or any wrong association in its relationship, how can I fix it?
java hibernate jpa orm hibernate-mapping
java hibernate jpa orm hibernate-mapping
edited Mar 23 at 18:36
Andronicus
7,56531835
7,56531835
asked Mar 22 at 9:11
kevinBrandkevinBrand
237
237
The property is namedpk.books
, notpk.BOOK
. Andbooks
should really be rebamed tobook
, since it references a single book.
– JB Nizet
Mar 22 at 9:17
Why pk.books is used is defined in the books in the composition id class ,isn't it? If it is, It is similar to define pk.order because of defining order in the compostion id class. I can try it. When I have an issue , I inform you about it.
– kevinBrand
Mar 22 at 9:56
add a comment |
The property is namedpk.books
, notpk.BOOK
. Andbooks
should really be rebamed tobook
, since it references a single book.
– JB Nizet
Mar 22 at 9:17
Why pk.books is used is defined in the books in the composition id class ,isn't it? If it is, It is similar to define pk.order because of defining order in the compostion id class. I can try it. When I have an issue , I inform you about it.
– kevinBrand
Mar 22 at 9:56
The property is named
pk.books
, not pk.BOOK
. And books
should really be rebamed to book
, since it references a single book.– JB Nizet
Mar 22 at 9:17
The property is named
pk.books
, not pk.BOOK
. And books
should really be rebamed to book
, since it references a single book.– JB Nizet
Mar 22 at 9:17
Why pk.books is used is defined in the books in the composition id class ,isn't it? If it is, It is similar to define pk.order because of defining order in the compostion id class. I can try it. When I have an issue , I inform you about it.
– kevinBrand
Mar 22 at 9:56
Why pk.books is used is defined in the books in the composition id class ,isn't it? If it is, It is similar to define pk.order because of defining order in the compostion id class. I can try it. When I have an issue , I inform you about it.
– kevinBrand
Mar 22 at 9:56
add a comment |
2 Answers
2
active
oldest
votes
It's because mappedBy
is used to indicate a field, that is used for mapping (name of that field). You have probably used column names. If you want to use mappedBy
, you need to declare bidirectional relationship and on the child side use @JoinColumn
. Then you can pass the name of that field into mappedBy
.
Suppose you have the following in OrderDetailId
:
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "BOOK_ID")
private Book books;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "ORDER_ID")
private Orders order;
Then in Orders
:
@OneToMany(mappedBy = "order", cascade=CascadeType.ALL)
private Set<OrderDetail> orderDetails = new HashSet<OrderDetail>();
and in Book
:
@OneToMany(mappedBy = "pk.books", cascade=CascadeType.ALL)
private Set<OrderDetail> orderDetails = new HashSet<OrderDetail>();
I'd already tried to add @JoinColumn before sharing the post (@JoinColumn(name="BOOK_ID")) but nothing changed.
– kevinBrand
Mar 22 at 9:58
together withmappedBy
fixed?
– Andronicus
Mar 22 at 10:04
How can I do it (together with mappedBy fixed?)
– kevinBrand
Mar 22 at 10:23
Can you paste the code with@JoinColumn
? I'll show you then
– Andronicus
Mar 22 at 10:25
I changed the code.
– kevinBrand
Mar 22 at 10:34
|
show 1 more comment
You have to use columns for mapping.
But in you code for OrderDetailId
, you were used another entity references.
For better design you have to use simple Id's in ID class
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55296234%2fhibernate-foreign-key-problems%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's because mappedBy
is used to indicate a field, that is used for mapping (name of that field). You have probably used column names. If you want to use mappedBy
, you need to declare bidirectional relationship and on the child side use @JoinColumn
. Then you can pass the name of that field into mappedBy
.
Suppose you have the following in OrderDetailId
:
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "BOOK_ID")
private Book books;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "ORDER_ID")
private Orders order;
Then in Orders
:
@OneToMany(mappedBy = "order", cascade=CascadeType.ALL)
private Set<OrderDetail> orderDetails = new HashSet<OrderDetail>();
and in Book
:
@OneToMany(mappedBy = "pk.books", cascade=CascadeType.ALL)
private Set<OrderDetail> orderDetails = new HashSet<OrderDetail>();
I'd already tried to add @JoinColumn before sharing the post (@JoinColumn(name="BOOK_ID")) but nothing changed.
– kevinBrand
Mar 22 at 9:58
together withmappedBy
fixed?
– Andronicus
Mar 22 at 10:04
How can I do it (together with mappedBy fixed?)
– kevinBrand
Mar 22 at 10:23
Can you paste the code with@JoinColumn
? I'll show you then
– Andronicus
Mar 22 at 10:25
I changed the code.
– kevinBrand
Mar 22 at 10:34
|
show 1 more comment
It's because mappedBy
is used to indicate a field, that is used for mapping (name of that field). You have probably used column names. If you want to use mappedBy
, you need to declare bidirectional relationship and on the child side use @JoinColumn
. Then you can pass the name of that field into mappedBy
.
Suppose you have the following in OrderDetailId
:
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "BOOK_ID")
private Book books;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "ORDER_ID")
private Orders order;
Then in Orders
:
@OneToMany(mappedBy = "order", cascade=CascadeType.ALL)
private Set<OrderDetail> orderDetails = new HashSet<OrderDetail>();
and in Book
:
@OneToMany(mappedBy = "pk.books", cascade=CascadeType.ALL)
private Set<OrderDetail> orderDetails = new HashSet<OrderDetail>();
I'd already tried to add @JoinColumn before sharing the post (@JoinColumn(name="BOOK_ID")) but nothing changed.
– kevinBrand
Mar 22 at 9:58
together withmappedBy
fixed?
– Andronicus
Mar 22 at 10:04
How can I do it (together with mappedBy fixed?)
– kevinBrand
Mar 22 at 10:23
Can you paste the code with@JoinColumn
? I'll show you then
– Andronicus
Mar 22 at 10:25
I changed the code.
– kevinBrand
Mar 22 at 10:34
|
show 1 more comment
It's because mappedBy
is used to indicate a field, that is used for mapping (name of that field). You have probably used column names. If you want to use mappedBy
, you need to declare bidirectional relationship and on the child side use @JoinColumn
. Then you can pass the name of that field into mappedBy
.
Suppose you have the following in OrderDetailId
:
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "BOOK_ID")
private Book books;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "ORDER_ID")
private Orders order;
Then in Orders
:
@OneToMany(mappedBy = "order", cascade=CascadeType.ALL)
private Set<OrderDetail> orderDetails = new HashSet<OrderDetail>();
and in Book
:
@OneToMany(mappedBy = "pk.books", cascade=CascadeType.ALL)
private Set<OrderDetail> orderDetails = new HashSet<OrderDetail>();
It's because mappedBy
is used to indicate a field, that is used for mapping (name of that field). You have probably used column names. If you want to use mappedBy
, you need to declare bidirectional relationship and on the child side use @JoinColumn
. Then you can pass the name of that field into mappedBy
.
Suppose you have the following in OrderDetailId
:
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "BOOK_ID")
private Book books;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "ORDER_ID")
private Orders order;
Then in Orders
:
@OneToMany(mappedBy = "order", cascade=CascadeType.ALL)
private Set<OrderDetail> orderDetails = new HashSet<OrderDetail>();
and in Book
:
@OneToMany(mappedBy = "pk.books", cascade=CascadeType.ALL)
private Set<OrderDetail> orderDetails = new HashSet<OrderDetail>();
edited Mar 22 at 15:54
answered Mar 22 at 9:17
AndronicusAndronicus
7,56531835
7,56531835
I'd already tried to add @JoinColumn before sharing the post (@JoinColumn(name="BOOK_ID")) but nothing changed.
– kevinBrand
Mar 22 at 9:58
together withmappedBy
fixed?
– Andronicus
Mar 22 at 10:04
How can I do it (together with mappedBy fixed?)
– kevinBrand
Mar 22 at 10:23
Can you paste the code with@JoinColumn
? I'll show you then
– Andronicus
Mar 22 at 10:25
I changed the code.
– kevinBrand
Mar 22 at 10:34
|
show 1 more comment
I'd already tried to add @JoinColumn before sharing the post (@JoinColumn(name="BOOK_ID")) but nothing changed.
– kevinBrand
Mar 22 at 9:58
together withmappedBy
fixed?
– Andronicus
Mar 22 at 10:04
How can I do it (together with mappedBy fixed?)
– kevinBrand
Mar 22 at 10:23
Can you paste the code with@JoinColumn
? I'll show you then
– Andronicus
Mar 22 at 10:25
I changed the code.
– kevinBrand
Mar 22 at 10:34
I'd already tried to add @JoinColumn before sharing the post (@JoinColumn(name="BOOK_ID")) but nothing changed.
– kevinBrand
Mar 22 at 9:58
I'd already tried to add @JoinColumn before sharing the post (@JoinColumn(name="BOOK_ID")) but nothing changed.
– kevinBrand
Mar 22 at 9:58
together with
mappedBy
fixed?– Andronicus
Mar 22 at 10:04
together with
mappedBy
fixed?– Andronicus
Mar 22 at 10:04
How can I do it (together with mappedBy fixed?)
– kevinBrand
Mar 22 at 10:23
How can I do it (together with mappedBy fixed?)
– kevinBrand
Mar 22 at 10:23
Can you paste the code with
@JoinColumn
? I'll show you then– Andronicus
Mar 22 at 10:25
Can you paste the code with
@JoinColumn
? I'll show you then– Andronicus
Mar 22 at 10:25
I changed the code.
– kevinBrand
Mar 22 at 10:34
I changed the code.
– kevinBrand
Mar 22 at 10:34
|
show 1 more comment
You have to use columns for mapping.
But in you code for OrderDetailId
, you were used another entity references.
For better design you have to use simple Id's in ID class
add a comment |
You have to use columns for mapping.
But in you code for OrderDetailId
, you were used another entity references.
For better design you have to use simple Id's in ID class
add a comment |
You have to use columns for mapping.
But in you code for OrderDetailId
, you were used another entity references.
For better design you have to use simple Id's in ID class
You have to use columns for mapping.
But in you code for OrderDetailId
, you were used another entity references.
For better design you have to use simple Id's in ID class
answered Mar 22 at 9:25
Ajmal MuhammadAjmal Muhammad
545220
545220
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55296234%2fhibernate-foreign-key-problems%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The property is named
pk.books
, notpk.BOOK
. Andbooks
should really be rebamed tobook
, since it references a single book.– JB Nizet
Mar 22 at 9:17
Why pk.books is used is defined in the books in the composition id class ,isn't it? If it is, It is similar to define pk.order because of defining order in the compostion id class. I can try it. When I have an issue , I inform you about it.
– kevinBrand
Mar 22 at 9:56