Questions about the hibernate field mapping @colunm and the problem of jpa camel case conversion _ lowercase

Premise description: The dto mentioned in the text is to be mapped with the table of the database: the dto class file has the following annotations at the beginning:

@SelectBeforeUpdate(value = true)
@DynamicInsert(value = true)
@DynamicUpdate(value = true)
@javax.persistence.Entity
@javax.persistence.Table(name = "映射的数据库表名字")
@AccessType("field")

Because the data source was temporarily changed to another place, I found that the table structure of the database to be retrieved was inconsistent with the original one, which meant that the query sql and the encapsulated dto had to be rewritten. I made a backup of these files, and then backed up Modified above.

First look at the dao layer: (because there are hundreds of fields in the table, so select * is used, so there is no alias for each field)

public List<QlsxData_YW_Dto> getData(String strlast,String strtoday){
		String sql = String.format("select * from xxxxx where  UPDATE_DATE > '%s' and UPDATE_DATE < '%s' ",strlast,strtoday);
		List<QlsxData_YW_Dto> list = this.createSqlQuery(sql).setResultTransformer(Transformers.aliasToBean(QlsxData_YW_Dto.class)).list();
		return list;
	}
The query results are encapsulated into dto. Because the new data source has only a few more fields than the original data source, the new dto---
QlsxData_YW_Dto

Just add the mapping of these fields on the basis of the original dto.

The fields in the library are as follows:

Is_TouZiP;
NoUnify_Do;
UNUnifyDo_Other;
IsHasOwnFlow;
bszn_url;
NoSuit_ReasonDesc;
business_regulate;

The code in the added dto is as follows:

        @Column(name = "is_touzip")
private String istouzip;
@Column(name = "nounify_do")
private String nounifydo;
@Column(name = "ununifydo_other")
private String ununifydoother;
@Column(name = "ishasownflow") 
private String ishasownflow;
@Column(name = "bszn_url")
private String bsznurl;
@Column(name = "nosuit_reasondesc")
private String nosuitreasondesc;
@Column(name = "business_regulate")

private String businessregulate;

Obviously, I changed the value of the name attribute in the annotation to the all lowercase form of the field name. This is so because,

    I ran into this problem a few days ago. At that time, the database field names also had both uppercase and lowercase camel case and underscores. I write the value of the name attribute in the annotation the same as the database field name, and then the name written in the private String below is the standard camel case name defined by myself. When this dto is stored in a new table using hibernate's save method, there will be an exception. Says the xxxx field cannot be found. For example, if the original field is zxcVb_d, it will report that the field zxc_vb_d cannot be found. I checked it and said that it is the jpa specification, which converts uppercase letters to _ and lowercase letters. So the value of the name attribute in the annotation was changed to all lowercase of the field name, and this problem was solved. For details, please refer to this article: Hibernate JAP fields automatically add underscores,


However, it reported an error. The exception says that the setter method of Is_TouZiP cannot be found. Is_TouZiP is a field of the database, not the is_touzip I wrote.

The problem is also obvious, that is, because the query result is not mapped to the field, the encapsulation fails. It's very depressing, it was solved like this last time, and this time it failed directly.


Because there are several @Transient fields in this dto, I saw an article that said. Don't have the get and set methods of these fields in dto. After trying it, it is still the same. In the end, there is no way. You don't need to name your own. Just change these fields to the following. (The name of dto is the same as the database field name. No need to annotate @colunm)

,as follows:

        @Column
private String Is_TouZiP;
@Column
private String NoUnify_Do;
@Column
private String UNUnifyDo_Other;
@Column
private String IsHasOwnFlow;
@Column
private String bszn_url;
@Column
private String NoSuit_ReasonDesc;
@Column

private String business_regulate;

problem solved. But the way I wrote it at the beginning (learning from the last experience) was fine before, why not this time? I tried a few experiments:

one,

The name of the database is: Is_SimplePunish

@Column(name = "is_simplepunish")
private String Is_SimplePunish;                  无异常。


two,

The name of the database is: Service_Sub_Kind

@Column(name = "Service_Sub_Kind")

private String Service_Sub_Kind; no exception

------------------------------

Database field: QL_Sub_Kind, 

@Column(name = "ql_sub_kind")

private String QL_Sub_Kind ; no exceptions.

When I change the above to

@Column(name = "ql_sub_kind")

private String tt;

run, an exception occurred.


Through the above experiments, it is found that the property name that looks like a private string must be consistent with the database field name.

[Note that the same is true in the grass-based link article released earlier:


], and @Column(name = seems to have nothing to do with it.

Then the jpa specification I just mentioned is the situation where capital letters are changed to _+ lowercase. When will it appear? The above reference article has already been said, but the Service_Sub_Kind field in the second experiment just now is also a hump. Why didn't this exception occur? do not understand. Continue the experiment:

Database FeeBasis field:

@Column(name = "feebasis")

private String FeeBasis;

change to

@Column(name = "FeeBasis")

private String FeeBasis;

Sure enough, an exception occurred: data insertion failed:


Regarding the issue of the jpa specification, it seems that it is not a standard camel case name such as Service_Sub_Kind that is not affected, but such as FeeBasis with this standard camel case name, the problem arises.


Regarding these two issues, I have a summary of my own exploration:

@Column(name = "feebasis")//It is recommended to write all lowercase directly.

private String FeeBasis;//This must be consistent with the corresponding field name of the database, otherwise there will be an exception that the setter method of the corresponding field cannot be found.


Hibernate is to be learned systematically. o(╯□╰)o



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324851685&siteId=291194637