0% found this document useful (0 votes)
26 views3 pages

One Tomany Cascade

The document shows an example of one-to-many mapping with cascade operations in JPA. It defines Branch and Hospital entities, with a hospital having a list of branches. The driver code saves a new hospital with 3 branch entities to the database using cascade operations.

Uploaded by

300allinone
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views3 pages

One Tomany Cascade

The document shows an example of one-to-many mapping with cascade operations in JPA. It defines Branch and Hospital entities, with a hospital having a list of branches. The driver code saves a new hospital with 3 branch entities to the database using cascade operations.

Uploaded by

300allinone
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package oneTomanyCascade;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity(name = "branchesCascade")
public class Branch {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String manager;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getManager() {
return manager;
}
public void setManager(String manager) {
this.manager = manager;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}

=================================================================================

package oneTomanyCascade;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

@Entity(name = "hospitalCascade")
public class Hospital {
@Id
private int id;
private String ceo;
private String name;

@OneToMany
@Cascade(CascadeType.ALL)
private List<Branch> branches;

public int getId() {


return id;
}

public void setId(int id) {


this.id = id;
}

public String getCeo() {


return ceo;
}

public void setCeo(String ceo) {


this.ceo = ceo;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public List<Branch> getBranches() {


return branches;
}

public void setBranches(List<Branch> branches) {


this.branches = branches;
}

========================================================================

package oneTomanyCascade;

import java.util.ArrayList;
import java.util.List;

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

public class HospitalDriver {

public static void main(String[] args) {


EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory("dev");
EntityManager entityManager =
entityManagerFactory.createEntityManager();
EntityTransaction entityTransaction = entityManager.getTransaction();

Hospital hospital=new Hospital();


hospital.setId(1);
hospital.setName("Apollo");
hospital.setCeo("Harsh");

Branch branch2=new Branch();


branch2.setManager("RamanaSir");
branch2.setAddress("Lspider");

Branch branch1=new Branch();


branch1.setManager("VaraSir");
branch1.setAddress("Jspider");

Branch branch3=new Branch();


branch3.setManager("RavishSir");
branch3.setAddress("Qspider");

List<Branch> branchList = new ArrayList<Branch>();


branchList.add(branch1);
branchList.add(branch2);
branchList.add(branch3);

hospital.setBranches(branchList);

entityTransaction.begin();
entityManager.persist(hospital);
entityTransaction.commit();
}

You might also like