Unable to replace Default ID Column Default Data JPA

综合编程 2018-02-11 阅读原文

I recently refactored our database schema to have the id columns in each of our tables to be in the format "tableName" + ID, IE "SettingsID" instead of just "id" across all tables. Now my spring-data backend is breaking when I try to fetch anything from one of those tables. It complains that there is an "invalid column name 'id'". I assume what I need to do is map my new name of the ID column to what spring data wants to be the id column, but I havent been successful so far.

I think the only configuration needed would happen within my entity object. Here is my Settings.java entity object class:

@Entity
@Table(name = Settings.TABLE_NAME)
public class Settings extends AbstractPersistable {
public static final String TABLE_NAME = "SETTINGS";

@AttributeOverride(name = "id", column = @Column(name="settingsID"))
private long settingsID;

@Column(name = "CUSTOMERID")
private String customerID;

@Column(name = "MERCHANTID")
private String merchantID;
...
....
}

And just in case it matters, (Which I don't think it does) here is the function I am calling when this error is thrown:

@Repository
public interface SettingsDAO extends CrudRepository {

/**
 * Finds the entry in the Settings table for a given customerID
 * @param customerID   The customerID to search for
 * @return             The settings object for the given customer ID
 */
Settings findOneByCustomerID(String customerID);

}

The error I get is (beside from the generic hibernate error saying it cant extract the resultset) is

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name 'id'.

After checking out this post, I realized that I wasn't supposed to add the @AttributeOverride annotation on the field, but the class itself. That way, spring data will not try to double map the id field. My new entity object looks like this:

@Entity
@Table(name = Settings.TABLE_NAME)
@AttributeOverride(name="id", [email protected](name="SETTINGSID"))
public class Settings extends AbstractPersistable {
public static final String TABLE_NAME = "SETTINGS";

@Column(name = "CUSTOMERID")
private String customerID;

@Column(name = "MERCHANTID")
private String merchantID;
...

Notice there is no field for id now.

Hello, buddy!

责编内容by:Hello, buddy!阅读原文】。感谢您的支持!

您可能感兴趣的

用MyEclipse JPA创建项目(二) MyEclipse 3.15 Style——在线购买低至75折! 火爆开抢>> 【 MyEclipse最新版下载 】 本教程介绍了M...
spring-boot-data-jpa nativeQuery 详解 问题: 1:一般情况下,用spring-boot data jpa查询sql稍有点多,本来一条sql解决的事情,一定要几条, 如何优化成一条sql解决,还能...
Spring Data Jpa(分页、Specification、Criteria) 分页的主要接口与类 PagingAndSortingRepository 继承自 CrudRepository 接口,提供了排序以及分页查询能...
Spring MVC and JPA – Save to a Database In this blog post, I am going to share with you how to create a very simple REST...
The Sad State of JSR-310 Support in JPA 2.2 Offically JPA 2.2 supports JSR-310 / Java Date Time API datatypes. In order to ...