Hibernate send wrong sql in spring boot and sql server


#1

I use reverse engine generated .java code and .hbm.xml file, the table name is “UploadedFile”, entity name in hbm.xml and in java code is “UploadedFile”, however when I try to insert one entry in to DB, it send out the sql “insert into testdb.dbo.uploaded_file (upload_file_file_name, upload_file_file_content) values (?, ?)”, and then I get the exception that “testdb.dbo.uploaded_file” is invalid.
The UploadedFile.hbm.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Generated 2018-11-11 21:10:52 by Hibernate Tools 5.3.6.Final --><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping auto-import="true" default-access="property" default-cascade="none" default-lazy="true">
    <class catalog="TestDB" dynamic-insert="false" dynamic-update="false" mutable="true" name="com.yp.entity.UploadedFile" optimistic-lock="version" polymorphism="implicit" schema="dbo" select-before-update="false" table="UploadedFile">
        <id name="uploadFileId" type="java.lang.Integer">
            <column name="UploadFile_ID"/>
            <generator class="identity"/>
        </id>
        <property generated="never" lazy="false" name="uploadFileFileName" optimistic-lock="true" type="string" unique="false">
            <column length="20" name="UploadFile_FileName" not-null="true"/>
        </property>
        <property generated="never" lazy="false" name="uploadFileFileContent" optimistic-lock="true" type="binary" unique="false">
            <column name="UploadFile_FileContent" not-null="true"/>
        </property>
    </class>
</hibernate-mapping>
public class UploadedFile implements java.io.Serializable {

	private Integer uploadFileId;
	private String uploadFileFileName;
	private byte[] uploadFileFileContent;

	public UploadedFile() {
	}
        //getter,setter
}

The database table:

create table UploadFile(
	UploadFile_ID int identity primary key,
	UploadFile_FileName varchar(20) not null,
	UploadFile_FileContent varbinary(2048) not null,
);

Why the entity name in sql becomes “uploaded_file”?


#2

Maybe you are using a custom naming strategy. Try to debug it and see where the table name comes from.


#3

I did not specify the naming strategy, Do you mean to debug the hibernate source code?, If your meaning is checking my own code, then my code is very simple:

`@Repository
public interface FileUploadDao extends CrudRepository<UploadedFile, Integer> {
	
}`

and I called the default save method in this dao.


#4

I meant debugging the Hibernate code so that you get s better idea where that table name comes from. Or you could try to replicate it using a test case so we can also see the problem.


#5

Problem solved, It is caused by naming strategy, a good post is here:spring boot generates wrong sql


#6

sorry I didn’t read your answer carefully, I should use my search engine more efficient, I have just wasted my own time.