OnetoOne
UserDetails.java
@Entity @Getter @Setter @Table(name="USER_DETAIL") public class UserDetails { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int UserId; private String UserName; @OneToOne private Vehicles veh; }
Vehicles.java
@Entity @Getter @Setter public class Vehicles { @Id @GeneratedValue private int vehicleId; private String vehicleName; }
CreateTable.java
public static void main(String[] args) { UserDetails objUserDetail1 = new UserDetails(); objUserDetail1.setUserName("Mugil Vannan"); Vehicles objVeh = new Vehicles(); objVeh.setVehicleName("Suzuki"); objUserDetail1.setVeh(objVeh); UserDetails objUserDetail2 = new UserDetails(); objUserDetail2.setUserName("Mani"); Vehicles objVeh2 = new Vehicles(); objVeh2.setVehicleName("Maruthi"); objUserDetail2.setVeh(objVeh2); SessionFactory sessionFact = createSessionFactory(); Session session = sessionFact.openSession(); session.beginTransaction(); session.save(objUserDetail1); session.save(objVeh); session.save(objUserDetail2); session.save(objVeh2); session.getTransaction().commit(); session.close(); }
OnetoMany
UserDetails.java
@Getter @Setter @Entity @Table(name="USER_DETAIL") public class UserDetails { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int UserId; private String UserName; @OneToMany @JoinTable(joinColumns=@JoinColumn(name="USER_ID"), inverseJoinColumns=@JoinColumn(name="VEHICLE_ID")) private List<Vehicles> arrVeh = new ArrayList<Vehicles>(); }
Vehicles.java
@Getter @Setter @Entity public class Vehicles { @Id @GeneratedValue private int vehicleId; private String vehicleName; }
CreateTables.java
public static void main(String[] args) { UserDetails objUserDetail1 = new UserDetails(); objUserDetail1.setUserName("Mugil Vannan"); Vehicles objVeh = new Vehicles(); objVeh.setVehicleName("Suzuki"); objUserDetail1.getArrVeh().add(objVeh); Vehicles objVeh2 = new Vehicles(); objVeh2.setVehicleName("Maruthi"); objUserDetail1.getArrVeh().add(objVeh2); SessionFactory sessionFact = createSessionFactory(); Session session = sessionFact.openSession(); session.beginTransaction(); session.save(objUserDetail1); session.save(objVeh); session.save(objVeh2); session.getTransaction().commit(); session.close(); }
ManytoOne
UserDetails.java
@Getter @Setter @Entity @Table(name="USER_DETAIL") public class UserDetails { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int UserId; private String UserName; }
Vehicles.java
@Getter @Setter @Entity public class Vehicles { @Id @GeneratedValue private int vehicleId; private String vehicleName; @ManyToOne private UserDetails objUserDetails; }
CreateTables.java
public static void main(String[] args) { Vehicles objVeh1 = new Vehicles(); objVeh1.setVehicleName("Suzuki"); Vehicles objVeh2 = new Vehicles(); objVeh2.setVehicleName("Maruthi"); UserDetails objUserDetail1 = new UserDetails(); objUserDetail1.setUserName("Mugil Vannan"); objVeh1.setObjUserDetails(objUserDetail1); objVeh2.setObjUserDetails(objUserDetail1); SessionFactory sessionFact = createSessionFactory(); Session session = sessionFact.openSession(); session.beginTransaction(); session.save(objUserDetail1); session.save(objVeh1); session.save(objVeh2); session.getTransaction().commit(); session.close(); }
ManytoMany
UserDetails.java
@Getter @Setter @Entity @Table(name="USER_DETAIL") public class UserDetails { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int UserId; private String UserName; @ManyToMany private List<Vehicles> arrVehicles = new ArrayList<Vehicles>(); }
Vehicles.java
@Getter @Setter @Entity public class Vehicles { @Id @GeneratedValue private int vehicleId; private String vehicleName; @ManyToMany(mappedBy="arrVehicles") private List<UserDetails> arrUserDetails = new ArrayList<UserDetails>(); }
CreateTables.java
public static void main(String[] args) { Vehicles objVeh1 = new Vehicles(); objVeh1.setVehicleName("Suzuki"); Vehicles objVeh2 = new Vehicles(); objVeh2.setVehicleName("Maruthi"); UserDetails objUserDetail1 = new UserDetails(); objUserDetail1.setUserName("Mugil Vannan"); UserDetails objUserDetail2 = new UserDetails(); objUserDetail2.setUserName("Mani"); objVeh1.getArrUserDetails().add(objUserDetail1); objVeh1.getArrUserDetails().add(objUserDetail2); objUserDetail1.getArrVehicles().add(objVeh1); objUserDetail1.getArrVehicles().add(objVeh2); SessionFactory sessionFact = createSessionFactory(); Session session = sessionFact.openSession(); session.beginTransaction(); session.save(objUserDetail1); session.save(objUserDetail2); session.save(objVeh1); session.save(objVeh2); session.getTransaction().commit(); session.close(); }