Forums Register Login

JPA-Hibernate One-to-One Bidirectional Relationship issue

+Pie Number of slices to send: Send
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$$
We can fix it! We just need some baling wire, some WD-40, a bit of duct tape and this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 3591 times.
Similar Threads
Hibernate Newbie - 1 to Many Relation Tutorial
Bidirectional @OneToOne with JoinTable
Hibernate exception in OneToOne mapping
Hibernate is driving me crazy !
Persistance issue
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 14:47:09.