【问题标题】:Inheritance and @AttributeOverride on @Id field@Id 字段上的继承和@AttributeOverride
【发布时间】:2015-05-02 21:30:57
【问题描述】:

我有一个我需要手动设置 ID (PK) 的实体。 我有一些用于 Audit 和 PK 的 Abstract @MappedSuperclass,我仍然想使用它。所以我的想法是覆盖 id 列以摆脱 @GeneratedValue(strategy = GenerationType.AUTO.

所以我有这样的东西:

@Entity
@Table(name = Constants.MERCHANT_PREFIX + "MERCHANT")
@Cacheable(false)
public class Merchant extends AbstractAuditable<String, Long> {
    @AttributeOverride(name = "id", column = @Column(name="ID"))
    private Long id;
    @Override
    public Long getId() {
        return id;
    }
    @Override
    public void setId(Long id) {
        this.id = id;
    }
}

@MappedSuperclass
@EntityListeners(value = { AuditingEntityListener.class })
public abstract class AbstractAuditable<U, PK extends Serializable> extends AbstractPersistable<PK> {
...
}

@MappedSuperclass
public abstract class AbstractPersistable<PK extends Serializable> implements Persistable<PK> {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private PK id;
    public PK getId() {
        return id;
    }
    protected void setId(final PK id) {
        this.id = id;
    }
}

它工作正常(服务器启动并大摇大摆)

但是我的集成测试失败了,我在下面有这个错误。 你知道为什么吗?我想也许我不应该以那种方式使用@AttributeOverride...而且我想找到一种工作方式,让我的 Merchant 类仍在扩展我的抽象类(审计和 PK)

org.springframework.dao.InvalidDataAccessResourceUsageException: 
could not prepare statement; 
        SQL [insert into MER_MERCHANT (ID, CREATED_BY, CREATED_DATE, LAST_MODIFIED_BY, LAST_MODIFIED_DATE, ALLOW_ACCESS, id) 
values (default, ?, ?, ?, ?, ?, ?)]; 
        nested exception is org.hibernate.exception.SQLGrammarException: 
could not prepare statement

单元测试:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {ApplicationTest.class})
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class,
        TransactionalTestExecutionListener.class,
        DbUnitTestExecutionListener.class })
@DatabaseSetup(value = "classpath:merchantData.xml")
@DatabaseTearDown(value="classpath:merchantData.xml", type = DatabaseOperation.DELETE_ALL)
public class RestMerchantControllerTest extends AbstractControllerTest {

    @Test
    public void testCreateMerchant() throws Exception {
        final Long merchantId = 2L;
        final Boolean allowAccess = true;
        MerchantCreateVO merchantCreateVO = StubVOBuilder.buildMerchantCreateVO(allowAccess);
        String requestBody = JsonUtils.jsonFormat(merchantCreateVO);

        mvc.perform(post("/merchants/" + merchantId).contentType(MediaType.APPLICATION_JSON).content(requestBody)).andExpect(status().isOk());
    }

}

和ApplicationTest:(ApplicationTest很像我们用来设置JPA的Application)

@Configuration
@ComponentScan(value = "uk.co.xxx.yyy", excludeFilters = @ComponentScan.Filter(value=Configuration.class, type = FilterType.ANNOTATION ))
@EnableWebMvc
@EnableTransactionManagement
@PropertySources({@PropertySource("classpath:hibernate.properties"), @PropertySource("classpath:junit-persistence.properties"), @PropertySource("classpath:application-test.properties")})
@EnableJpaRepositories(basePackages = "uk.co.xxx.yyy.merchant.data.repository")
@EnableJpaAuditing(auditorAwareRef = "springSecurityAuditorAware")
public class ApplicationTest {
@Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
...
}
 @Bean
    public JpaTransactionManager transactionManager() {
...
}

}

在服务器上我有这个插入:

insert 
into
    MER_MERCHANT
    (CREATED_BY, CREATED_DATE, LAST_MODIFIED_BY, LAST_MODIFIED_DATE, ALLOW_ACCESS, id) 
values
    (?, ?, ?, ?, ?, ?)

并通过集成测试:

insert 
into
    MER_MERCHANT
    (ID, CREATED_BY, CREATED_DATE, LAST_MODIFIED_BY, LAST_MODIFIED_DATE, ALLOW_ACCESS, id) 
values
    (default, ?, ?, ?, ?, ?, ?)

【问题讨论】:

  • attributeOverride 将覆盖列名。它不一定会禁用任何值生成器
  • 是但没有 attributeOverride ,如果我进行插入,无论我之前设置的 id 和 @attributeOverride 我将在数据库中设置我的 id(通过通过序列)。只是测试不起作用

标签: jpa spring-data-jpa


【解决方案1】:

所以我的想法是覆盖 id 列以摆脱 @GeneratedValue(策略 = GenerationType.AUTO.

@AttributeOverride 注释用于向 JPA 发出信号,表明您希望更改属性名称到列名称的默认映射。如果您正在开发一个 JPA 应用程序,该应用程序与旧系统互操作,其字段或列的名称与您的名称不同。

在 JavaDoc 中,@AttributeOverride 是...

  • 用于覆盖基本(无论是显式还是默认)属性或字段或 Id 属性或字段的映射。

  • 可应用于扩展映射超类的实体或嵌入字段或属性,以覆盖由映射超类或可嵌入类(或其属性之一的可嵌入类)定义的基本映射或 id 映射.

不幸的是,这个注解只对可以通过@Column API 引用的属性或字段映射有效……而@Column 注解基本上只允许您更改字段名称的映射。您不能使用@AttributeOverride 更改字段类型或任何其他映射。因此,@GeneratedValue 映射将继续被@MappedSuperclass 的所有子类继承。

【讨论】:

  • 你是对的,我们终于改变并选择添加第二个抽象类与'@MappedSuperClass' 几次前不同的id策略,我忘了更新答案,那里没有办法正确使用“@AttributeOverride”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多