英文:
Unable to find column with logical name while setting bean property
问题
I am not able to start my project this error pop up:
Exception encountered during context initialization - cancelling
refresh attempt:
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'commentRepository' defined in
com.vivek.discussion.repository.CommentRepository defined in
@EnableJpaRepositories declared on
JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot
resolve reference to bean 'jpaMappingContext' while setting bean
property 'mappingContext'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'jpaMappingContext': Invocation of init method
failed; nested exception is org.hibernate.MappingException: Unable to
find column with logical name: postId in
org.hibernate.mapping.Table(post) and its related supertables and
secondary tables
User.class
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.time.Instant;
@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long userId;
@NotBlank(message = "Username is required")
private String username;
@NotBlank(message = "Password is required")
private String password;
@Email
@NotEmpty(message = "Email is required")
private String email;
private Instant created;
private boolean enabled;
}
Post.class
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.lang.Nullable;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.time.Instant;
@Data
@Entity
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@NotBlank(message = "Cannot left this blank")
private String postName;
@Nullable
private String url;
@Nullable
@Lob
private String description;
private int voteCount;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "userid", referencedColumnName = "userid")
private User user;
private Instant createdDate;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id", referencedColumnName = "id")
private Subreddit subreddit;
}
Comment.class
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import java.time.Instant;
import static javax.persistence.FetchType.LAZY;
import static javax.persistence.GenerationType.IDENTITY;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Comment {
@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
@NotEmpty
private String text;
@ManyToOne(fetch = LAZY)
@JoinColumn(name = "postId", referencedColumnName = "postId")
private Post post;
private Instant createdDate;
@ManyToOne(fetch = LAZY)
@JoinColumn(name = "userId", referencedColumnName = "userId")
private User user;
}
CommentRepo.class
import com.vivek.discussion.model.Comment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CommentRepository extends JpaRepository<Comment, Long> {
}
Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.vivek</groupId>
<artifactId>discussion</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>discussion</name>
<description>Application where student can discuss on any topic they want</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Error :
英文:
I am not able to start my project this error pop up :
> Exception encountered during context initialization - cancelling
> refresh attempt:
> org.springframework.beans.factory.BeanCreationException: Error
> creating bean with name 'commentRepository' defined in
> com.vivek.discussion.repository.CommentRepository defined in
> @EnableJpaRepositories declared on
> JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot
> resolve reference to bean 'jpaMappingContext' while setting bean
> property 'mappingContext'; nested exception is
> org.springframework.beans.factory.BeanCreationException: Error
> creating bean with name 'jpaMappingContext': Invocation of init method
> failed; nested exception is org.hibernate.MappingException: Unable to
> find column with logical name: postId in
> org.hibernate.mapping.Table(post) and its related supertables and
> secondary tables
User.class
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.time.Instant;
@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long userId;
@NotBlank(message = "Username is required")
private String username;
@NotBlank(message = "Password is required")
private String password;
@Email
@NotEmpty(message = "Email is required")
private String email;
private Instant created;
private boolean enabled;
}
Post.class
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.lang.Nullable;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.time.Instant;
@Data
@Entity
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@NotBlank(message = "Cannot left this blank")
private String postName;
@Nullable
private String url;
@Nullable
@Lob
private String description;
private int voteCount;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "userid", referencedColumnName = "userid")
private User user;
private Instant createdDate;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id", referencedColumnName = "id")
private Subreddit subreddit;
}
Comment.class
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import java.time.Instant;
import static javax.persistence.FetchType.LAZY;
import static javax.persistence.GenerationType.IDENTITY;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Comment {
@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
@NotEmpty
private String text;
@ManyToOne(fetch = LAZY)
@JoinColumn(name = "postId", referencedColumnName = "postId")
private Post post;
private Instant createdDate;
@ManyToOne(fetch = LAZY)
@JoinColumn(name = "userId", referencedColumnName = "userId")
private User user;
}
CommentRepo.class
import com.vivek.discussion.model.Comment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CommentRepository extends JpaRepository<Comment,Long> {
}
Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.vivek</groupId>
<artifactId>discussion</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>discussion</name>
<description>Application where student can discuss on any topic they want</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Error :
答案1
得分: 1
在你的 Comment
类中,将这一行更改为:
@JoinColumn(name = "postId", referencedColumnName = "id")
英文:
In your Comment
class, Change this line:
@JoinColumn(name = "postId", referencedColumnName = "postId")
To this one
@JoinColumn(name = "postId", referencedColumnName = "id")
答案2
得分: 1
你的参考列名称在你的代码中引起了问题,
在评论类中,注解 "join column" 应该被修改。
将参考的列名称更改为 "id",因为在你提到的 "post" 类中,你已将其指定为 "id"。
英文:
Your reference column name caused the problem in your code,
in comment class the annotation join column should be modified.
Change the referenced column name to id as in the class post you have mentioned it as id.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论