我想在Springboot项目中实现外键,但是它给了我以下错误

时间:2018-10-09 11:57:26

标签: mysql hibernate spring-boot

Whitelabel错误页面

This application has no explicit mapping for /error, so you are seeing 
this as a 
fallback.Tue Oct 10 17:11:14 IST 2018 There was an unexpected error 
(type=Internal Server 
Error, status=500). could not execute statement; SQL [n/a]; nested 
exception is org.
hibernate.exception.SQLGrammarException: could not execute statement.

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

办公室桌子(表名称:办公室)

   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列属性自动递增

1 个答案:

答案 0 :(得分: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;