Mysql 我想在我的Springboot项目中实现外键,但是我得到了以下错误

Mysql 我想在我的Springboot项目中实现外键,但是我得到了以下错误,mysql,hibernate,spring-boot,Mysql,Hibernate,Spring Boot,白标错误页 此应用程序没有/error的显式映射,因此您将其视为回退。2018年10月10日星期二17:11:14出现意外错误(type=Internal Server error,status=500)。无法执行语句;SQL[n/a];嵌套异常为组织。 hibernate.exception.SQLGrammarException:无法执行语句 STS错误: Before changing the code com.mysql.jdbc.exceptions.jdbc4.MySQLInteg

白标错误页

此应用程序没有/error的显式映射,因此您将其视为回退。2018年10月10日星期二17:11:14出现意外错误(type=Internal Server error,status=500)。无法执行语句;SQL[n/a];嵌套异常为组织。
hibernate.exception.SQLGrammarException:无法执行语句

STS错误:

Before changing the code

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolation
Exception
: Cannot add or update a child row: a foreign key constraint fails  
(workdemo.officeinfo, CONSTRAINT idFOREIGN KEY (id) REFERENCES mytable 
(id))

After implementing joincolumn

 org.springframework.beans.factory.BeanCreationException: Error 
 creating bean with name 'entityManagerFactory' defined in class path 
 resource [org/springframework/boot/autoconfigure/orm/jpa/
 HibernateJpaConfiguration.class]: Invocation of init method failed; 
 nested exception is org.hibernate.AnnotationException: No identifier 
 specified for entity:com.infidata.modal.MyTable
POJO(还有getter和setter的值 产生的价值)

MyTable.java

package com.infidata.modal;
@Entity
public class MyTable {

 }
数据库(数据库名称为workdemo)

用户表(表名:mytable)

办公桌(桌名:Office)


office表中的id(外键)应参照student id列属性自动递增

问题在于如何定义实体类:

@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
使用JPA时,必须指定关系的目标实体,而不是数据库中的字段。 您的定义只是告诉hibernate生成一个与实际实体不对应的int值。 应该是这样的:

@ManyToOne
@JoinColumn(name = "id" )
private User user;
您的办公对象将是

@Entity
@Table(name = "officeinfo")
public class Office {

    @Id
    private int sno;
    private String batchno;

    @ManyToOne
    @JoinColumn(name = "id")
    private User user;

    private String fees;
    private String reciptno;
    private String trainer;
   // getters and setters;
 }
请确保@Id仅在sno上,并且在其他字段上没有,否则它将失败,并出现复合密钥异常。请从对象中删除id,它是用户的外键,由以下人员处理:

 @ManyToOne
 @JoinColumn(name = "id")
 private User user;

我确实实现了您的代码,但它给了我以下错误:
org.springframework.beans.factory.BeanCreationException:创建名为“entityManagerFactory”的bean时出错,该bean在类路径资源[org/springframework/boot/autoconfigure/orm/jpa/hibernatejbaconfiguration.class]中定义:初始化方法调用失败;嵌套异常为org.hibernate.AnnotationException:@OneToOne或@ManyToOne(位于com.infidata.modal.Office.myTable上)引用未知实体:com.infidata.modal.myTable。
是否为myTable创建了一个用@entity注释的类?与OfficeClassi类似,它现在给出了
org.springframework.beans.factory.BeanCreationException:创建名为“entityManagerFactory”的bean时出错,该bean在类路径资源[org/springframework/boot/autoconfigure/orm/jpa/hibernatejbaconfiguration.class]中定义:调用init方法失败;嵌套异常为org.hibernate.AnnotationException:未为实体com.infidata.modal.MyTable指定标识符。
您在MyTable中的Id字段中没有@Id。我的POJO位于Ofice.java
@entity@Table(name=“officeinfo”)公共类Office中{@Id private int sno;private String batchno;@manytone@Id@JoinColumn(name=“Id”)private MyTable MyTable;//我为新代码实现创建了这个额外的类private String fees;private String reciptno;private String trainer;}
@ManyToOne
@JoinColumn(name = "id" )
private User user;
@Entity
@Table(name = "officeinfo")
public class Office {

    @Id
    private int sno;
    private String batchno;

    @ManyToOne
    @JoinColumn(name = "id")
    private User user;

    private String fees;
    private String reciptno;
    private String trainer;
   // getters and setters;
 }
 @ManyToOne
 @JoinColumn(name = "id")
 private User user;