【发布时间】:2015-06-18 11:26:14
【问题描述】:
我有一个类 Customer,它与 Subscription 具有 OneToOne 双向关系:
@Entity
@Table(name = "customers")
public class Customer{
@OneToOne(mappedBy="customer",cascade = CascadeType.ALL)
private Subscription currentSubscription;
}
@Entity
@Table(name = "subscriptions")
public class Subscription {
@Id
@Column(columnDefinition = "INT8",name="id", unique=true, nullable=false)
@GeneratedValue(generator="gen")
@GenericGenerator(name="gen", strategy="foreign", parameters=@Parameter(name="property", value="customer"))
private Long id;
@OneToOne
@PrimaryKeyJoinColumn
private Customer customer;
}
现在,当我创建一个带有订阅的客户并在该客户上调用 persist 时,它很好地将订阅保存到数据库中。但是,当我已经保留了一个客户并想要添加订阅时,它会失败并出现以下错误:
引起:org.hibernate.id.IdentifierGenerationException:尝试 从 null 一对一属性分配 id [com.qmino.miredot.portal.domain.Subscription.customer]
我写了一个测试来解释我想要实现的目标:
@Test
public void shouldCascadeUpdateSubscription(){
Customer owner = customerRepository.save(CustomerMother.getCustomer(false));
Subscription subscription = SubscriptionBuilder.create()
.setBillingDayOfMonth(LocalDate.now().getDayOfMonth())
.setSubscriptionPlan(subscriptionPlan)
.build();
subscription.setCustomer(owner);
owner.setCurrentSubscription(subscription);
customerRepository.save(owner);
Customer result = customerRepository.findOne(owner.getId());
assertThat(result.getCurrentSubscription(),is(notNullValue()));
assertThat(result.getCurrentSubscription().getId(),is(result.getId()));
}
我哪里做错了?
【问题讨论】:
-
你说实体是双向链接的吗?它看起来不像。您的订阅没有客户。所以 'mappedBy="customer" ' 是多余的。
-
我的错,出错了。现在应该是正确的。
-
subscription.setAccountDetails(owner);为什么不遵循 EJB 规范? Hibernate 应该有一个合适的 setter/getter。所以“setAccoundDetails”应该是“setCustomer”/“getCustomer”
-
再次出错,我错误地输入了错误的类,并且忘记了setter。我很抱歉。
-
那么parameters=@Parameter(name="property", value="accountDetails" 也应该是客户?请改正所有错别字
标签: java hibernate jpa spring-data one-to-one