aspose file tools*
The moose likes Object Relational Mapping and the fly likes JPA-Hibernate One-to-One Bidirectional Relationship issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA-Hibernate One-to-One Bidirectional Relationship issue" Watch "JPA-Hibernate One-to-One Bidirectional Relationship issue" New topic
Author

JPA-Hibernate One-to-One Bidirectional Relationship issue

Alpha Omega
Greenhorn

Joined: Jan 09, 2011
Posts: 7
Hello, I am having issue understanding hibernate's persistence flow for one of the sample code from Enterprise Java Beans 3 5th Edition book

I am using MySql as DB

Basically, i am trying to create a one-2-one bi directional relationship between Customer & CreditCard entities where Customer table in db will hold CreditCard's PK & vice versa.

All said & done, the db rows do not reflect this...

----------Customer Data ----------

CUSTOMER_ID,FIRST_NAME,LAST_NAME,ADDRESS_ID,CREDIT_CARD_ID
12,"first name","last name",19,4

------------- CreditCard Data-------
CREDIT_CARD_ID,EXP_DATE,NUMBER,NAME,CUSTOMER_ID
4,2011-11-09,123-456-7890,first name last name,null

As you can see CreditCard row does not hold reference key to Customer (CUSTOMER_ID is null)

Not sure why is this happening, as i understand both the db tables will hold references to each other. Or else how would it be bi directional relationship??

Thanks,
N.D.

----- Entity POJOs -------------

@Entity
@Table(name="CUSTOMER",schema="alpha")
public class Customer implements Serializable {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="CUSTOMER_ID")
private Long customerId;

@Column(name="FIRST_NAME")
private String firstName;

@Column(name="LAST_NAME")
private String lastName;

@OneToOne(cascade={CascadeType.ALL})
@JoinColumn(name="ADDRESS_ID")
private Address address;

@OneToOne(cascade={CascadeType.ALL})
@JoinColumn(name="CREDIT_CARD_ID")
private CreditCard creditCard;

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;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Long getCustomerId() {
return customerId;
}
public CreditCard getCreditCard() {
return creditCard;
}
public void setCreditCard(CreditCard creditCard) {
this.creditCard = creditCard;
}
}

@Entity
@Table(name="CREDIT_CARD",schema="alpha")
public class CreditCard implements Serializable {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="CREDIT_CARD_ID")
private Long creditCardId;

@Column(name="EXP_DATE")
private Date expirationDate;

@Column(name="NUMBER")
private String number;

@Column(name="NAME")
private String name;

@OneToOne(mappedBy="creditCard",optional=false,orphanRemoval=true)
private Customer customer;

public Date getExpirationDate() {
return expirationDate;
}

public void setExpirationDate(Date expirationDate) {
this.expirationDate = expirationDate;
}

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 Long getCreditCardId() {
return creditCardId;
}

public Customer getCustomer() {
return customer;
}

public void setCustomer(Customer customer) {
this.customer = customer;
}

------ DB Tables --------
CREATE TABLE `customer` (
`CUSTOMER_ID` bigint(20) NOT NULL AUTO_INCREMENT,
`FIRST_NAME` varchar(45) NOT NULL,
`LAST_NAME` varchar(45) NOT NULL,
`ADDRESS_ID` int(11) NOT NULL,
`CREDIT_CARD_ID` bigint(20) NOT NULL,
PRIMARY KEY (`CUSTOMER_ID`),
KEY `FK_CUST_ADDRESS` (`ADDRESS_ID`),
KEY `FK_CUST_CC` (`CREDIT_CARD_ID`),
CONSTRAINT `FK_CUST_CC` FOREIGN KEY (`CREDIT_CARD_ID`) REFERENCES `credit_card` (`CREDIT_CARD_ID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `FK_CUST_ADDRESS` FOREIGN KEY (`ADDRESS_ID`) REFERENCES `address` (`ADDRESS_ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=ascii$$


CREATE TABLE `credit_card` (
`CREDIT_CARD_ID` bigint(20) NOT NULL AUTO_INCREMENT,
`EXP_DATE` date NOT NULL,
`NUMBER` varchar(20) NOT NULL,
`NAME` varchar(45) NOT NULL,
`CUSTOMER_ID` bigint(20) DEFAULT NULL,
PRIMARY KEY (`CREDIT_CARD_ID`),
KEY `FK_CC_CUSTOMER` (`CUSTOMER_ID`),
CONSTRAINT `FK_CC_CUSTOMER` FOREIGN KEY (`CUSTOMER_ID`) REFERENCES `customer` (`CUSTOMER_ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=ascii$$
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JPA-Hibernate One-to-One Bidirectional Relationship issue