休止状態の挿入中に正常に動作する次のエンティティがあります。
ユーザー.java
@Entity
@Table
@Inheritance(strategy = InheritanceType.JOINED)
public class User extends PersistentObject {
private static final long serialVersionUID = -1803375723276957167L;
// commenting this association allows update to work successfully.
@OneToOne(fetch = FetchType.LAZY, mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private ContactInfo contactInfo;
private String name;
}
次に、切り離されていると思われるエンティティに対して更新を実行しようとしましたが、実行中に null ポインター例外が発生しましcascadeOnUpdate
た。
例外
java.lang.NullPointerException
at org.hibernate.engine.spi.EntityEntry.getLoadedValue(EntityEntry.java:274)
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:236)
at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:165)
at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:132)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.cascadeOnUpdate(DefaultSaveOrUpdateEventListener.java:361)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:335)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:239)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:109)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:724)
at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:716)
at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:712)
at com.myapp.dao.GenericHibernateDAO.save(GenericHibernateDAO.java:279)
at com.myapp.service.StaffInfoServiceImpl.storeInfo(StaffInfoServiceImpl.java:74)
at com.myapp.service.StaffInfoServiceImpl.storeInfo(StaffInfoServiceImpl.java:1)
アップデート
ContactInfo.java
@Entity
@Table
@DynamicUpdate
public class ContactInfo implements Serializable {
private static final long serialVersionUID = 4485388013971134422L;
@Column
@Length(min = 3, max = 50)
private String currentCity;
@ManyToOne
@JoinColumn(name = "current_country_id")
private Country currentCountry;
@Column(columnDefinition = "char(10)")
private String currentPostCode;
@Column
@Length(min = 3, max = 50)
private String currentState;
@Column
@Length(min = 3, max = 50)
private String currentStreetOne;
@Column
@Length(min = 3, max = 50)
private String currentStreetThree;
@Column
@Length(min = 3, max = 50)
private String currentStreetTwo;
@Column
@Email
private String email;
@Column
@Length(max = 20)
private String fax;
@Column
@Length(max = 20)
private String homePhone;
@GenericGenerator(name = "generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "user"))
@Id
@GeneratedValue(generator = "generator")
@Column(name = "user_id", unique = true, nullable = false)
private Long id;
@Column
@Length(max = 20)
private String mobilePhone;
@Column
@Length(max = 20)
private String officePhone;
@Column
@Length(min = 3, max = 50)
private String permanentCity;
@ManyToOne
@JoinColumn(name = "permanent_country_id")
private Country permanentCountry;
@Column(columnDefinition = "char(10)")
private String permanentPostCode;
@Column
@Length(min = 3, max = 50)
private String permanentState;
@Column
@Length(min = 3, max = 50)
private String permanentStreetOne;
@Column
@Length(min = 3, max = 50)
private String permanentStreetThree;
@Column
@Length(min = 3, max = 50)
private String permanentStreetTwo;
@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn
private User user;
@Version
@Column(name = "version")
private Integer version = 0;
}
私の質問は、挿入中に例外がスローされず、更新中に例外がスローされないのはなぜですか? ここで何を見逃したのですか?