简体   繁体   中英

jpa hibernate mysql query broken

Hi I have a confusing error with my Spring-boot-JPA-Hibernate classes.

I used the JPA Dali tools to create the Entity classes from my SQL Schema. While using them (with Spring-boot-jpa / hibarnate) I have some strange problems with unmatched queries. Here is one example:

Properties:

hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://localhost:3306/users
spring.datasource.username=root
spring.datasource.password=root

Entity:

@Entity
@Table(name="n_user")
@NamedQuery(name="NUser.findAll", query="SELECT n FROM NUser n")
public class NUser implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private int id;

    private String imageUrl1;

    private String name_first;


    public NCaterer() {
    }

    public int getId() {
        return this.id;
    }

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


    public String getImageUrl1() {
        return this.imageUrl1;
    }

    public void setImageUrl1(String imageUrl1) {
        this.imageUrl1 = imageUrl1;
    }

    public String getName_first() {
        return this.name_first;
    }

    public void setName_first(String name_first) {
        this.name_first = name_first;
    }

}

Repository:

public interface UserRepo extends CrudRepository<NUser, Long> {

}

But Hibernate creates this query of it:

Hibernate:
    /* select
        generatedAlias0
    from
        NUser as generatedAlias0 */ select
            nuser0_.id as id1_0_,
            nuser0_.image_url1 as image_ur2_0_,
            nuser0_.name_first as name_firs3_0_
        from
            n_user nuser0_

The Problem here is the image_url1 it should be imageurl1 as described in the Entity class. This happens on several points.

Why do Hibernate transform CamelCase getters into camel_case in the query? And how can I config it not to do it?

It seems there have been many people experiencing the same problem in different circumstances. Maybe go and have a look at the different naming strategies below:

Naming Strategy One

Naming Strategy Two

Naming Strategy Three

You can however change the configuration of your hibernation. Have a look here for further help with regards to that. Or, go and have a look at this post and see how someone changed it using the above methods.

If you want to specify column name by your own use @Column annotation for your fields.

Example:

@Column(name = "imageUrl1")
private String imageUrl1;
@Column(name = "nameFirst")
private String name_first;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM