When doing orm.xml, I found that the order of Joincolumn and Cascade should be in order, otherwise it will encountered some problem.
Here is my definition of some classes, Customer and CreditCard.
package com.titan.domain;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name=”CUSTOMER_TABLE”)
public class Customer implements java.io.Serializable {
private int id;
private String firstName;
private String lastName;
private Address address;
private CreditCard creditCard;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@OneToOne(cascade={CascadeType.ALL})
@JoinColumn(name=”ADDRESS_ID”)
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@OneToOne(cascade={CascadeType.ALL})
@JoinColumn(name=”CREDIT_CARD_ID”)
public CreditCard getCreditCard() {
return creditCard;
}
public void setCreditCard(CreditCard creditCard) {
this.creditCard = creditCard;
}
}
package com.titan.domain;
import java.util.Date;
import javax.persistence.Id;
import javax.persistence.OneToOne;
public class CreditCard implements java.io.Serializable {
private int id;
private Date expiration;
private String number;
private String name;
private String organization;
private Customer customer;
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getExpiration() {
return expiration;
}
public void setExpiration(Date expiration) {
this.expiration = expiration;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOrganization() {
return organization;
}
public void setOrganization(String organization) {
this.organization = organization;
}
@OneToOne(mappedBy=”creditCard”)
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
Here is my orm.xml, placed below META-INF
<entity-mappings
xmlns=”http://java.sun.com/xml/ns/persistence/orm”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd”
version=”1.0″>
<entity class=”com.titan.domain.Customer” access=”PROPERTY”>
<attributes>
<id name=”id”>
<generated-value/>
</id>
<one-to-one name=”creditCard”
target-entity=”com.titan.domain.CreditCard”
fetch=”LAZY”>
<join-column name=”CREDIT_CARD_ID”/>
<cascade>
<cascade-all />
</cascade>
</one-to-one>
</attributes>
</entity>
<entity class=”com.titan.domain.CreditCard” access=”PROPERTY”>
<attributes>
<id name=”id”>
<generated-value/>
</id>
<one-to-one name=”customer”
target-entity=”com.titan.domain.Customer”
mapped-by=”creditCard”/>
</attributes>
</entity>
</entity-mappings>
To note that, cascade element should be below of join-column element, and inside cascade element it contains cascade-all, cascade-persist, cascade-merge, cascade-remove, cascade-refresh
Reference Site: http://markmail.org/message/73xeygrwcslyekwn