【问题标题】:Spring boot JPA Unknown UserSpring Boot JPA 未知用户
【发布时间】:2018-06-01 16:12:56
【问题描述】:

我想集成 Spring 和 JPA 我正在使用 Spring Boot。首先,我尝试使用 EntityManager 创建 SessionFactory,但我做不到。我已经在这里Spring boot inject EntityManagerFactory in configuration class 提出了这个问题。这些建议对我没有帮助,它仍然不起作用。所以现在我决定尝试通过 entityManager 来坚持,但是当我坚持我的对象时,我得到了一个错误。

java.lang.IllegalArgumentException: Unknown entity: kz.training.springrest.entity.User

有我的用户类

import lombok.*;

import javax.persistence.*;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Entity
@Table(name = "users")
public class User {

    @Id
    @SequenceGenerator(name = "user_id_seq_gen", sequenceName = "user_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_id_seq_gen")
    private Long id;
    @Column
    private String username;
    @Column
    private String password;
}

服务类

package kz.training.springrest.service;

import kz.training.springrest.entity.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;

@Service
public class UserService {

    @PersistenceContext
    private EntityManager entityManager;

    @Transactional
    public void insertUser(User user) {
        entityManager.persist(user);
    }

}

跑步者

@SpringBootApplication
@ComponentScan(basePackages="kz.training.springrest")
public class SpringrestApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringrestApplication.class, args);
    }
}

数据库配置以防万一)还没有

@Configuration
@EnableTransactionManagement

public class DatabaseConfiguration {

}

我的依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</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-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.2</version>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>

    </dependencies>

属性

spring.datasource.url= jdbc:postgresql://localhost:5432/ring
spring.datasource.username=postgres
spring.datasource.password=root

spring.jpa.show-sql = false
spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.hibernate.naming.implicit-strategy = org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl
spring.jpa.properties.hibernate.format_sql=true


#Note: The last two properties on the code snippet above were added to suppress an annoying exception
# that occurs when JPA (Hibernate) tries to verify PostgreSQL CLOB feature.
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false

有什么想法吗?

【问题讨论】:

  • 您能否通过编辑您的 qs 并粘贴堆栈跟踪来共享完整的异常堆栈跟踪。你也没有在你的application.properties中设置spring.datasource.driverClassName=org.postgresql.Driver

标签: java spring-boot jpa


【解决方案1】:

为您的SpringrestApplication添加@EntityScan

@SpringBootApplication
@ComponentScan(basePackages="kz.training.springrest")
@EntityScan("kz.training.springrest.entity")
public class SpringrestApplication {

【讨论】:

  • 很好)非常感谢)
猜你喜欢
  • 2022-01-13
  • 2021-02-09
  • 2019-06-14
  • 2017-11-20
  • 2017-09-17
  • 2018-07-10
  • 2019-03-02
  • 2021-04-02
  • 2017-07-04
相关资源
最近更新 更多