JPA使用数据库相关bug笔记

306 阅读2分钟

下划线错误

修改前

pojo

@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Entity
@Table(name = "card")
public class Card{
    @Id
    @Column(name = "id", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private int uid;
    private String ubankname;
    private String n_test;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
        Card card = (Card) o;
        return id != null && Objects.equals(id, card.id);
    }

    @Override
    public int hashCode() {
        return getClass().hashCode();
    }
}

repository

@Repository
public interface CardRepository extends JpaRepository<Card,Long> {

    public List<Card> findAllByN_test(String test);
}

test

   @Test
    void error(){
        cardRepository.findAllByN_test("22").forEach(System.out::println);
    }

错误信息:从下划线处分割

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cardRepository' defined in com.example.error.repository.CardRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.List com.example.error.repository.CardRepository.findAllByN_test(java.lang.String); Reason: Failed to create query for method public abstract java.util.List com.example.error.repository.CardRepository.findAllByN_test(java.lang.String)! No property 'n' found for type 'Card' Did you mean ''id''; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.example.error.repository.CardRepository.findAllByN_test(java.lang.String)! No property 'n' found for type 'Card' Did you mean ''id''

修改后

pojo

@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Entity
@Table(name = "card")
public class Card{
    @Id
    @Column(name = "id", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private int uid;
    private String ubankname;
    @Column(name = "n_test")
    private String ntest;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
        Card card = (Card) o;
        return id != null && Objects.equals(id, card.id);
    }

    @Override
    public int hashCode() {
        return getClass().hashCode();
    }
}

repository

@Repository
public interface CardRepository extends JpaRepository<Card,Long> {

    public List<Card> findAllByNtest(String test);
}

test

    @Test
    void error(){
        cardRepository.findAllByNtest("22").forEach(System.out::println);
    }

驼峰命名

修改前

pojo

@Data
@Entity
@Table(name = "tuser")
public class User {

    @Id
    private int uid;
    private String uName;
    private String uSex;

}

repository

@Repository
public interface UserRepository extends JpaRepository<User,Integer> {
}

test

    void contextLoads() {
        userRepository.findAll().forEach(System.out::println);
    }

报错信息:驼峰转下划线

Hibernate: 
    select
        user0_.uid as uid1_1_,
        user0_.u_name as u_name2_1_,
        user0_.u_sex as u_sex3_1_ 
    from
        tuser user0_
2022-08-02 16:01:31.673  WARN 13580 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1054, SQLState: 42S22
2022-08-02 16:01:31.673 ERROR 13580 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : Unknown column 'user0_.u_name' in 'field list'

解决方案一

application.yml加上

 jpa:
    hibernate:
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

解决方案二

pojo改为

@Data
@Entity
@Table(name = "tuser")
public class User {
    @Id
    private int uid;
    @Column(name = "uname")
    private String uName;
    @Column(name = "usex")
    private String uSex;
}