Spring Boot Hibernate关联映射报错:EntityManagerFactory编译错误
Hey there, let's tackle this EntityManagerFactory issue you're hitting. From your description, it's clear the problem ties directly to the @ManyToOne relationship in your Student entity—since non-foreign-key tables work fine, the association mapping is likely the culprit.
First: Fix the @JoinColumn Annotation in Your Student Entity
Looking at your code, the @JoinColumn has an unnecessary table attribute that's confusing JPA. The @ManyToOne annotation already knows to reference the table defined by your College entity, so specifying table = "college" here is redundant and can break relationship resolution.
Here's the corrected Student entity code:
package com.example.demo.Model; import javax.persistence.*; @Entity @Table(name = "student") public class Student { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "sid") private int id; @Column(name = "sname") private String name; @Column(name = "sroll") private int roll; // Removed the unnecessary 'table' attribute from @JoinColumn @ManyToOne @JoinColumn(name = "cid") private College college; // Getters and setters public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getRoll() { return roll; } public void setRoll(int roll) { this.roll = roll; } public College getCollege() { return college; } public void setCollege(College college) { this.college = college; } }
Second: Verify Your College Entity is Properly Configured
Make sure your College entity has correct JPA annotations and a valid primary key—this is critical for the @ManyToOne relationship to work:
package com.example.demo.Model; import javax.persistence.*; @Entity @Table(name = "college") public class College { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "cid") private int id; // Add other fields as needed @Column(name = "cname") private String name; // Getters and setters public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Third: Double-Check Your Project Configuration
Dependency Setup: Ensure your
pom.xml(Maven) includes the correct Spring Boot JPA starter for 1.5.9.RELEASE:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- Add your database driver, e.g., MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> <!-- Compatible with Spring Boot 1.5.x --> </dependency>Database Properties: Confirm your
application.propertieshas valid database settings (adjust for your DB):spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name?useSSL=false&useUnicode=true&characterEncoding=utf8 spring.datasource.username=your_db_username spring.datasource.password=your_db_password spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
Fourth: Clear IntelliJ IDEA Cache
Sometimes IDE caching can cause false compilation errors. Try this:
- Go to
File > Invalidate Caches / Restart - Select
Invalidate and Restartto clear the cache and reload your project
After making these changes, rebuild your project—this should resolve the EntityManagerFactory compilation error and let your relationship-based CRUD operations work as expected.
内容的提问来源于stack exchange,提问作者sam

