【问题标题】:I want to implement foreign key in my Springboot project but I get the following errors我想在我的 Springboot 项目中实现外键,但出现以下错误
【发布时间】:2018-10-09 11:57:26
【问题描述】:

白标错误页面

此应用程序没有 /error 的显式映射,因此您将其视为后备。2018 年 10 月 10 日星期二 17:11:14 IST 2018 出现意外错误(类型=内部服务器错误,状态=500)。无法执行语句; SQL [不适用];嵌套异常是 org。
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 的值,也 生成值)

Office.java
package com.infidata.modal;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="officeinfo")
public class Office {
@Id
private int sno;
private String batchno;
@ManyToOne
@JoinColumn(name = "id" )
private MyTable myTable;
private String fees;
private String reciptno;
private String trainer;

public Office() {

}

public int getSno() {
    return sno;
}
public void setSno(int sno) {
    this.sno = sno;
}
public String getBatchno() {
    return batchno;
}
public void setBatchno(String batchno) {
    this.batchno = batchno;
}

public String getFees() {
    return fees;
}
public void setFees(String fees) {
    this.fees = fees;
}
public String getReciptno() {
    return reciptno;
}
public void setReciptno(String reciptno) {
    this.reciptno = reciptno;
}
public String getTrainer() {
    return trainer;
}
public void setTrainer(String trainer) {
    this.trainer = trainer;
}
public Office(String batchno,String fees, String reciptno,String trainer) {
    super();
    this.batchno = batchno;
    this.fees = fees;
    this.reciptno = reciptno;
    this.trainer=trainer;
}
@Override
public String toString() {
    return "Office [sno=" + sno + ", batchno=" + batchno + ",fees=" + fees
            + ", reciptno=" + reciptno + ",trainer=" + trainer + "]";
}
}

MyTable.java

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

 }

数据库(数据库名是workdemo)

用户表(表名:mytable)

  CREATE TABLE `mytable`

  ( `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) NOT NULL,
  `mobile` varchar(10) NOT NULL,
  `email` varchar(45) NOT NULL,
  `college` varchar(45) NOT NULL,
  `branch` varchar(45) NOT NULL,
  `semester` varchar(45) NOT NULL,
  `address` varchar(105) NOT NULL,
  `internship` varchar(45) NOT NULL,
  `batch` varchar(45) NOT NULL,
  `startdate` varchar(45) NOT NULL,
  `enddate` varchar(45) NOT NULL,
   PRIMARY KEY (`id`)
   )
   ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 
   COLLATE=utf8mb4_0900_ai_ci

办公桌(表名:office)

   CREATE TABLE `office`
  (`sno` int(11) NOT NULL AUTO_INCREMENT,
 `batchno` varchar(45) NOT NULL,
 `id` int(11) NOT NULL,
 `fees` varchar(45) NOT NULL,
 `reciptno` varchar(45) NOT NULL,
  PRIMARY KEY (`sno`),
  KEY `id_idx` (`id`),
  CONSTRAINT `id` FOREIGN KEY (`id`) REFERENCES `mytable` (`id`)
)
 ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

office表中的id(外键)应参照学生id列属性自增

【问题讨论】:

    标签: mysql hibernate spring-boot


    【解决方案1】:

    问题是你如何定义实体类:

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    

    当您使用 JPA 时,您必须指定关系的目标实体,而不是数据库中的字段。 您的定义只是告诉休眠生成一个不对应于真实实体的 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,它是 User 的外键,由以下人员处理:

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

    【讨论】:

    • 我确实实现了你的代码,但它给了我这个错误: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: @OneToOne or @ManyToOne on com.infidata.modal.Office.myTable references an unknown entity: com.infidata.modal.MyTable.
    • 你为 MyTable 创建了一个用@Entity 注释的类吗?与Office类类似
    • 我现在给了这个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.
    • MyTable 的 id 字段中没有 @Id
    • 我的 POJO 在 Office.java @Entity @Table(name="officeinfo") public class Office { @Id private int sno; private String batchno; @ManyToOne @Id @JoinColumn(name = "id" ) private MyTable myTable; // i created this extra class for the new code implementation private String fees; private String reciptno; private String trainer; }
    猜你喜欢
    • 2019-10-23
    • 2021-07-14
    • 2013-10-08
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多