Total members 11895 |It is currently Wed Dec 18, 2024 10:03 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





If you have a one to one relation between two entities , you can use cascade property to specify cascade type . If you set it to Cascade.All . Saving one entity will save the other one automatically . Look for the following example please :
Code:

package com
.codemiles.jpa;

import java.util.Date;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 * @author codemiles.
 */
public abstract class JPAService {
    private EntityManager entityManager;


    public void createPerson(String Id) {
    EntityManagerFactory factory = Persistence
            
.createEntityManagerFactory("persistenceUnitName");
    entityManager = factory.createEntityManager();
    entityManager.getTransaction().begin();
    Person person = new Person();
    
    person
.setId("231");
    person.setName("msi");
    person.setGender("Male");
    person.setBirthday(new Date());

    Bed bed = new Bed();
    bed.setId("23");
    bed.setHeight("23");
    bed.setWidth("342");
    bed.setType("Classic");
    bed.setProduction_date(new Date());
    bed.setPerson(person);
    person.setBed(bed);
    entityManager.persist(person);
    entityManager.getTransaction().commit();
    entityManager.close();
    factory.close();
     
     
    
}

 

}

 




Entity classes ( notice the cascade type ) :
Code:

package com
.codemiles.jpa;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@
Entity
@Table(name="PERSON")
public class Person implements java.io.Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    private String Id
;
    private String name;
    private Date birthday;
    private String gender;
    
    
@OneToOne(mappedBy="person",cascade=CascadeType.ALL) 
    private Bed bed
;
    
    public String getId
() {
        return Id;
    }
    public void setId(String id) {
        Id = id;
    }
    @Column(name="NAME")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Column(name="BIRTHDAT")
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    @Column(name="GENDER")
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public void setBed(Bed bed) {
    this.bed = bed;
    }
    public Bed getBed() {
    return bed;
    }
 
    
}

 



Code:

package com
.codemiles.jpa;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@
Entity
@Table(name="BED")
public class Bed implements java.io.Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    private String Id
;
    private String type;
    private Date production_date;
    private String width;
    private String height;
    @OneToOne
    
@PrimaryKeyJoinColumn
    private Person person
;
    public String getId() {
        return Id;
    }
    public void setId(String id) {
        Id = id;
    }
     
    
@Column(name="TYPE" ,nullable=false)
    public String getType() {
        return type;
    }
    
    public void setType
(String type) {
        this.type = type;
    }
    @Column(name="PRODUCTION")
    public Date getProduction_date() {
        return production_date;
    }
    public void setProduction_date(Date production_date) {
        this.production_date = production_date;
    }
    @Column(name="WIDTH")
    public String getWidth() {
        return width;
    }
    public void setWidth(String width) {
        this.width = width;
    }
    @Column(name="HEIGHT")
    public String getHeight() {
        return height;
    }
    public void setHeight(String height) {
        this.height = height;
    }
    public void setPerson(Person person) {
    this.person = person;
    }
    public Person getPerson() {
    return person;
    }
    
    
}

 


Note that :
Code:
cascade=ALL is equivalent to cascade={PERSIST, MERGE, REMOVE, REFRESH}




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Cascade.All and persisting related entities directly
 Persisting a class into two tables     -  
 SCJP Related Queries     -  
 cascade=CascadeType.REFRESH with many to one relation     -  
 Drawing circle using circle equation directly , on mouse     -  



Topic Tags

Java JPA
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com