Unable to create a table in hibernate using OneToMany and ManyToOne relationship(also unable to create Foreign Key)

huangapple go评论109阅读模式
英文:

Unable to create a table in hibernate using OneToMany and ManyToOne relationship(also unable to create Foreign Key)

问题

这里的代码主要用于确定OneToMany和ManyToOne的关系。我使用Spring Tool Suite 3.9.11与XAMPP以及Apache 7.5作为服务器。

以下是第一个文件/表customer.java的代码部分:

  1. @Entity
  2. @Table(name = "Customer")
  3. public class Customer {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.IDENTITY)
  6. private int cid;
  7. private String cname;
  8. private int ccontact;
  9. private String cemail;
  10. private String cpassword;
  11. @OneToMany(mappedBy = "customer")
  12. private List<OrderF> order;
  13. // 省略了其他方法和构造函数
  14. }

以下是第二个文件/表OrderF.java的代码部分:

  1. @Entity
  2. @Table(name="Order")
  3. public class OrderF {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.IDENTITY)
  6. private int food_id;
  7. private String foodName;
  8. @ManyToOne(cascade = CascadeType.PERSIST)
  9. @JoinColumn(name = "cid")
  10. private Customer customer;
  11. // 省略了其他方法和构造函数
  12. }

这是你的main.java的代码部分:

  1. public static void main(String[] args) {
  2. Customer c = new Customer();
  3. c.setCid(1);
  4. OrderF o = new OrderF();
  5. o.setCustomer(c);
  6. o.setFood_id(1);
  7. o.setFoodName("Java");
  8. SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
  9. Session session = sessionFactory.openSession();
  10. session.beginTransaction();
  11. session.save(o);
  12. session.getTransaction().commit();
  13. session.close();
  14. }

这是你的hibernate配置文件hibernate.cfg.xml的代码部分:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  8. <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/spring</property>
  9. <property name="hibernate.connection.username">root</property>
  10. <property name="hibernate.connection.password"></property>
  11. <property name="hibernate.show_sql">true</property>
  12. <property name="hbm2ddl.auto">create</property>
  13. <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
  14. <mapping class="com.bean.Customer"></mapping>
  15. <mapping class="com.bean.OrderF"></mapping>
  16. </session-factory>
  17. </hibernate-configuration>

这是你的控制台/错误日志部分。

英文:

This code here is simply used to determine the relation of OnetoMany and ManyToOne, I use Spring Tool suite 3.9.11 with XAMPP and Server as apache 7.5

This is first file/table customer.java

  1. package com.bean;
  2. import java.util.List;
  3. import javax.persistence.CascadeType;
  4. import javax.persistence.Entity;
  5. import javax.persistence.GeneratedValue;
  6. import javax.persistence.GenerationType;
  7. import javax.persistence.Id;
  8. import javax.persistence.JoinColumn;
  9. import javax.persistence.ManyToOne;
  10. import javax.persistence.OneToMany;
  11. import javax.persistence.Table;
  12. @Entity
  13. @Table(name = &quot;Customer&quot;)
  14. public class Customer {
  15. @Id
  16. @GeneratedValue(strategy = GenerationType.IDENTITY)
  17. private int cid;
  18. private String cname;
  19. private int ccontact;
  20. private String cemail;
  21. private String cpassword;
  22. @OneToMany(mappedBy = &quot;customer&quot;)
  23. private List&lt;OrderF&gt; order;
  24. public List&lt;OrderF&gt; getOrder() {
  25. return order;
  26. }
  27. public void setOrder(List&lt;OrderF&gt; order) {
  28. this.order = order;
  29. }
  30. public Customer() {
  31. super();
  32. }
  33. public int getCid() {
  34. return cid;
  35. }
  36. public void setCid(int cid) {
  37. this.cid = cid;
  38. }
  39. public String getCname() {
  40. return cname;
  41. }
  42. public void setCname(String cname) {
  43. this.cname = cname;
  44. }
  45. public int getCcontact() {
  46. return ccontact;
  47. }
  48. public void setCcontact(int ccontact) {
  49. this.ccontact = ccontact;
  50. }
  51. public String getCemail() {
  52. return cemail;
  53. }
  54. public void setCemail(String cemail) {
  55. this.cemail = cemail;
  56. }
  57. public String getCpassword() {
  58. return cpassword;
  59. }
  60. public void setCpassword(String cpassword) {
  61. this.cpassword = cpassword;
  62. }
  63. }

This is my second file/table OrderF.java

  1. package com.bean;
  2. import javax.persistence.CascadeType;
  3. import javax.persistence.Entity;
  4. import javax.persistence.GeneratedValue;
  5. import javax.persistence.GenerationType;
  6. import javax.persistence.Id;
  7. import javax.persistence.JoinColumn;
  8. import javax.persistence.ManyToOne;
  9. import javax.persistence.Table;
  10. @Entity
  11. @Table(name=&quot;Order&quot;)
  12. public class OrderF {
  13. @Id
  14. @GeneratedValue(strategy = GenerationType.IDENTITY)
  15. private int food_id;
  16. private String foodName;
  17. @ManyToOne(cascade = CascadeType.PERSIST)
  18. @JoinColumn(name = &quot;cid&quot;)
  19. private Customer customer;
  20. public int getFood_id() {
  21. return food_id;
  22. }
  23. public void setFood_id(int food_id) {
  24. this.food_id = food_id;
  25. }
  26. public String getFoodName() {
  27. return foodName;
  28. }
  29. public void setFoodName(String foodName) {
  30. this.foodName = foodName;
  31. }
  32. public Customer getCustomer() {
  33. return customer;
  34. }
  35. public void setCustomer(Customer customer) {
  36. this.customer = customer;
  37. }
  38. }

This is my main.java

  1. package com.bean;
  2. import org.hibernate.Session;
  3. import org.hibernate.SessionFactory;
  4. import org.hibernate.cfg.Configuration;
  5. public class MainClass {
  6. public static void main(String[] args) {
  7. Customer c=new Customer();
  8. c.setCid(1);
  9. OrderF o=new OrderF();
  10. o.setCustomer(c);
  11. o.setFood_id(1);
  12. o.setFoodName(&quot;Java&quot;);
  13. SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
  14. Session session=sessionFactory.openSession();
  15. session.beginTransaction();
  16. session.save(o);
  17. session.getTransaction().commit();
  18. session.close();
  19. }
  20. }

This is hibernate Configuration file hibernate.cfg.xml

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;!DOCTYPE hibernate-configuration PUBLIC
  3. &quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot;
  4. &quot;http://hibernate.org/dtd/hibernate-configuration-3.0.dtd&quot;&gt;
  5. &lt;hibernate-configuration&gt;
  6. &lt;session-factory&gt;
  7. &lt;property name=&quot;hibernate.connection.driver_class&quot;&gt;com.mysql.jdbc.Driver&lt;/property&gt;
  8. &lt;property name=&quot;hibernate.connection.url&quot;&gt;jdbc:mysql://localhost:3306/spring&lt;/property&gt;
  9. &lt;property name=&quot;hibernate.connection.username&quot;&gt;root&lt;/property&gt;
  10. &lt;property name=&quot;hibernate.connection.password&quot;&gt;&lt;/property&gt;
  11. &lt;property name=&quot;hibernate.show_sql&quot;&gt;true&lt;/property&gt;
  12. &lt;property name=&quot;hbm2ddl.auto&quot;&gt;create&lt;/property&gt;
  13. &lt;property name=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.MySQL5Dialect&lt;/property&gt;
  14. &lt;mapping class=&quot;com.bean.Customer&quot;&gt;&lt;/mapping&gt;
  15. &lt;mapping class=&quot;com.bean.OrderF&quot;&gt;&lt;/mapping&gt;
  16. &lt;/session-factory&gt;
  17. &lt;/hibernate-configuration&gt;

This is my Console/Error Log:

  1. version for the right syntax to use near &#39;Order drop foreign key FKk8oprs3d23xg7tjw95wxaft8l&#39; at line 1
  2. Hibernate: drop table if exists Customer
  3. Hibernate: drop table if exists Order
  4. Aug 14, 2020 12:04:34 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
  5. ERROR: HHH000389: Unsuccessful: drop table if exists Order
  6. Hibernate: create table Customer (cid integer not null auto_increment, ccontact integer not null, cemail varchar(255), cname varchar(255), cpassword varchar(255), primary key (cid))
  7. Aug 14, 2020 12:04:34 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
  8. ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near &#39;Order&#39; at line 1
  9. Hibernate: create table Order (food_id integer not null auto_increment, foodName varchar(255), cid integer, primary key (food_id))
  10. Aug 14, 2020 12:04:35 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
  11. ERROR: HHH000389: Unsuccessful: create table Order (food_id integer not null auto_increment, foodName varchar(255), cid integer, primary key (food_id))
  12. Hibernate: alter table Order add constraint FKk8oprs3d23xg7tjw95wxaft8l foreign key (cid) references Customer (cid)
  13. Aug 14, 2020 12:04:35 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
  14. ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near &#39;Order (food_id integer not null auto_increment, foodName varchar(255), cid integ&#39; at line 1
  15. Aug 14, 2020 12:04:35 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
  16. ERROR: HHH000389: Unsuccessful: alter table Order add constraint FKk8oprs3d23xg7tjw95wxaft8l foreign key (cid) references Customer (cid)
  17. Aug 14, 2020 12:04:35 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
  18. ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near &#39;Order add constraint FKk8oprs3d23xg7tjw95wxaft8l foreign key (cid) references Cu&#39; at line 1
  19. Aug 14, 2020 12:04:35 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
  20. INFO: HHH000230: Schema export complete
  21. Hibernate: insert into Order (cid, foodName) values (?, ?)
  22. Aug 14, 2020 12:04:35 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
  23. WARN: SQL Error: 1064, SQLState: 42000
  24. Aug 14, 2020 12:04:35 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
  25. ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near &#39;Order (cid, foodName) values (1, &#39;Java&#39;)&#39; at line 1
  26. Exception in thread &quot;main&quot; org.hibernate.exception.SQLGrammarException: could not execute statement
  27. at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)
  28. at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
  29. at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
  30. at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
  31. at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
  32. at org.hibernate.dialect.identity.GetGeneratedKeysDelegate.executeAndExtract(GetGeneratedKeysDelegate.java:57)
  33. at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:42)
  34. at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2792)
  35. at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3363)
  36. at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
  37. at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:597)
  38. at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:232)
  39. at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:213)
  40. at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:256)
  41. at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:317)
  42. at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:272)
  43. at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:178)
  44. at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:109)
  45. at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
  46. at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
  47. at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
  48. at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
  49. at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
  50. at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:679)

答案1

得分: 0

您的Order实体的表名是SQL引擎中的保留字,尝试重新命名表名。

  1. @Entity
  2. @Table(name="OrderF")
  3. public class OrderF {...}
英文:

Your Order entity's table name is a reserved word in the SQL engine, try renaming the table's name

  1. @Entity
  2. @Table(name=&quot;OrderF &quot;)
  3. public class OrderF {...}

答案2

得分: 0

作为重命名的替代方法,您可以使用SQL带引号的标识符:

  1. @Entity
  2. @Table(name="`Order`")
  3. public class OrderF {
  4. // ...
  5. }
英文:

As an alternative to renaming you can use SQL quoted identifiers:

  1. @Entity
  2. @Table(name=&quot;`Order`&quot;)
  3. public class OrderF {
  4. // ...
  5. }

huangapple
  • 本文由 发表于 2020年8月14日 14:46:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63407833.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定