0

I try to get database from sql server (Categories table from Northwind) but I got some mistake here:

Class: Category

and @Column(name = "column-name") not work with me, when I use JDBCTemplate will get null variable if properties name not equal column name in db :(

@Entity
@Table(name = "Categories")
public class Category {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "CategoryID")
    private Integer CategoryID;
    @Column(name = "CategoryName")
    private String CategoryName;
    @Column(name = "Description")
    private String Description;
    @Column(name = "Picture")
    private Byte[] Picture;
    
    public Category() {
        
    }
    public Category(Integer CategoryID, String CategoryName, String Description, Byte[] Picture) {
        this.CategoryID = CategoryID;
        this.CategoryName = CategoryName;
        this.Description = Description;
        this.Picture = Picture;
    }

    public Integer getCategoryID() {
        return CategoryID;
    }

    public String getCategoryName() {
        return CategoryName;
    }

    public String getDescription() {
        return Description;
    }

    public Byte[] getPicture() {
        return Picture;
    }
    public void setCategoryID(Integer categoryID) {
        CategoryID = categoryID;
    }
    public void setCategoryName(String categoryName) {
        CategoryName = categoryName;
    }
    public void setDescription(String description) {
        Description = description;
    }
    public void setPicture(Byte[] picture) {
        Picture = picture;
    }
        
}

Interface: CategoryRepository I did try with CrudRepository but got same error

public interface CategoryRepository extends JpaRepository<Category, Integer>{
    
}

Then, When I call CategoryRepository.findAll() the Hibernate select:

Hibernate: 
    select
        category0_.categoryid as category1_0_,
        category0_.category_name as category2_0_,
        category0_.description as descript3_0_,
        category0_.picture as picture4_0_ 
    from
        categories category0_

and will make com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name 'category_name'.

How can I solve this problem?

Update application.properties

spring.datasource.url=jdbc:sqlserver://localhost;databaseName=Northwind
spring.datasource.username=sa
spring.datasource.password=123
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto=none
LAM HOANG
  • 79
  • 1
  • 6
  • 2
    This is already answered here: https://stackoverflow.com/questions/29087626/entity-class-name-is-transformed-into-sql-table-name-with-underscores – panagiotis Aug 07 '21 at 09:59

0 Answers0