Java 使用SpringJPA和eclipseLink,UUID作为Id绑定在查询中作为对象引用
我正在尝试使用Spring和JPA。我在Oracle中有一个表,其中很少有字段是原始(16字节)类型的。我的实体如下所示:Java 使用SpringJPA和eclipseLink,UUID作为Id绑定在查询中作为对象引用,java,spring,jpa,spring-data,uuid,Java,Spring,Jpa,Spring Data,Uuid,我正在尝试使用Spring和JPA。我在Oracle中有一个表,其中很少有字段是原始(16字节)类型的。我的实体如下所示: @Entity @Table(name = "testTable") public class TestTable implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name = "ID") @Type(type =
@Entity
@Table(name = "testTable")
public class TestTable implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@Type(type = "uuid-binary")
private UUID id;
@Column(name = "TABLE_ID")
@Type(type = "uuid-binary")
private UUID tableId;
@Column(name = "STATUS")
private String status;
@Repository
public interface TestRepository extends JpaRepository<TestTable, UUID> {
TestTable getTestTableByTableId(@Param("table_id") UUId table_id);
}
我的存储库类如下所示:
@Entity
@Table(name = "testTable")
public class TestTable implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@Type(type = "uuid-binary")
private UUID id;
@Column(name = "TABLE_ID")
@Type(type = "uuid-binary")
private UUID tableId;
@Column(name = "STATUS")
private String status;
@Repository
public interface TestRepository extends JpaRepository<TestTable, UUID> {
TestTable getTestTableByTableId(@Param("table_id") UUId table_id);
}
当我在控制器中自动连接存储库并调用存储库中定义的方法时,JPA在后台创建SQL并尝试绑定参数,但它绑定的是对象引用而不是UUID,如下所示:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackageClasses = { TestRepository.class })
@EnableJpaAuditing
public class JpaConfig {
@Bean(name="entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
Map<String, Object> jpaProperties = new HashMap<>();
jpaProperties.put("eclipselink.weaving", "false");
jpaProperties.put("eclipselink.logging.parameters", "true");
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(this.getDataSource());
entityManagerFactoryBean.setPackagesToScan("com.test.entity");
entityManagerFactoryBean.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
entityManagerFactoryBean.setJpaVendorAdapter(this.vendorAdapter());
entityManagerFactoryBean.setJpaPropertyMap(jpaProperties);
return entityManagerFactoryBean;
}
@Bean
public JpaVendorAdapter vendorAdapter() {
EclipseLinkJpaVendorAdapter vendorAdapter = new EclipseLinkJpaVendorAdapter();
vendorAdapter.setDatabase(Database.ORACLE);
vendorAdapter.setShowSql(true);
return vendorAdapter;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
@Bean
public DriverManagerDataSource getDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
//datasource properties here
return dataSource;
}
@Bean()
public PlatformTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(this.entityManagerFactoryBean().getObject());
return transactionManager;
}
[EL Fine]: sql: 2016-08-10 09:17:22.306--ServerSession(1292029763)--Connection(678757862)--SELECT ID, TABLE_ID, STATUS FROM TESTTABLE WHERE (TABLE_ID= ?)
bind => [[B@2f6964a4]
我的问题是,为什么它要将其作为对象绑定?我该如何解决?提前感谢。您好,请尝试将类型从uuid binary更改为uuid char。@duardito如您所述更改了类型,仍尝试绑定为对象引用。因此,也许您应该使用varchar type。您将uuid存储为字符串。我以前使用过它,并以varchar的形式存储在DB中。@duardito这就是问题所在,我在DB中有这个字段作为原始(16字节)类型,并且这个配置在
HibernateJavaEndoraptor
中可以很好地工作,只是不能让它与EclipseLinkJPavendorAdapter