Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

JPA-Hibernate One-to-One Bidirectional Relationship issue

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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$$
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic