0% found this document useful (0 votes)
8 views8 pages

One To Many

The document contains Java code for a One-to-Many relationship between Student and Library entities using JPA and Hibernate. It includes the Student and Library classes with their respective attributes and methods, as well as an example class, OnetoManyExample, that demonstrates creating and persisting these entities. Additionally, it includes a Persistence.xml configuration file for setting up the database connection and Hibernate properties.

Uploaded by

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

One To Many

The document contains Java code for a One-to-Many relationship between Student and Library entities using JPA and Hibernate. It includes the Student and Library classes with their respective attributes and methods, as well as an example class, OnetoManyExample, that demonstrates creating and persisting these entities. Additionally, it includes a Persistence.xml configuration file for setting up the database connection and Hibernate properties.

Uploaded by

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

Student.

java

package com.jpa;
import java.util.List;

import javax.persistence.*;

//Example for ONE TO MANY

@Entity
public class Student {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int s_id;
private String s_name;

@OneToMany
private List<Library> libs;

public Student() {
super();

}
public Student(int s_id, String s_name, List<Library> libs) {
super();
this.s_id = s_id;
this.s_name = s_name;
this.libs = libs;
}

public int getS_id() {


return s_id;
}

public void setS_id(int s_id) {


this.s_id = s_id;
}

public String getS_name() {


return s_name;
}

public void setS_name(String s_name) {


this.s_name = s_name;
}

public List<Library> getLibs() {


return libs;
}
public void setLibs(List<Library> libs) {
this.libs = libs;
}

Library.java

package com.jpa;
import javax.persistence.*;
//Example for ONE TO MANY

@Entity
public class Library {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column
private int b_id;
private String b_name;

@ManyToOne
private Student stud;

public int getB_id() {


return b_id;
}

public void setB_id(int b_id) {


this.b_id = b_id;
}

public String getB_name() {


return b_name;
}
public void setB_name(String b_name) {
this.b_name = b_name;
}

public Student getStud() {


return stud;
}

public void setStud(Student stud) {


this.stud = stud;
}

public Library(int b_id, String b_name, Student


stud) {
super();
this.b_id = b_id;
this.b_name = b_name;
this.stud = stud;
}

public Library() {
super();
// TODO Auto-generated constructor stub
}

OnetoMany.java

package com.jpa;

//Example for ONE TO MANY

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.hibernate.Session;

public class OnetoManyExample {

public static void main(String[] args)


{
EntityManagerFactory
emf=Persistence.createEntityManagerFactory("Book");
EntityManager em=emf.createEntityManager();

em.getTransaction().begin();

//creating student object


Student st1=new Student();
st1.setS_name("Supriya");

// Student st2=new Student();


// st2.setS_name("Pallavi");

//creating no of Books in Library


Library lib1=new Library();
lib1.setB_name("Data Analysis");
lib1.setStud(st1); //issuing book to student1
//lib1.setStud(st2);

Library lib2=new Library();


lib2.setB_name("Java Stack");
lib2.setStud(st1);
// lib2.setStud(st2);

Library lib3=new Library();


lib3.setB_name("Spring");
lib3.setStud(st1);

em.persist(lib1);
em.persist(lib2);
em.persist(lib3);

//creating List to add the books


List<Library> list = new ArrayList<Library>();
list.add(lib1);
list.add(lib2);
list.add(lib3);

st1.setLibs(list);
// st2.setLibs(list);

em.persist(st1);
em.getTransaction().commit();

//fetching data from Tables


/*
Student s = ((Session) em).get(Student.class,9);
System.out.println("Student name-->"+ s.getS_name());

//for loop to display


for(Library li:s.getLibs())
{
System.out.print("Book name-->"+li.getB_name());
System.out.println("Book_iD-->"+li.getB_id());
}
*/
em.close();
emf.close();
}
}

Persistence.xml

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persi
stence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2
_1.xsd">

<persistence-unit name="Book">

<provider>org.hibernate.jpa.HibernatePersistenceProvi
der</provider>

<class>com.jpa.Student</class>
<class>com.jpa.Library</class>

<properties>
<property name="javax.persistence.jdbc.driver"
value="com.mysql.cj.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/jpadb"/>
<property name="javax.persistence.jdbc.user"
value="root"/>
<property
name="javax.persistence.jdbc.password"
value="admin"/>
<property name="hibernate.show_sql"
value="true" />
<property name="hibernate.format_sql"
value="true" />
<property name="hibernate.dialect"

value="org.hibernate.dialect.MySQL5Dialect" />

</properties>

</persistence-unit>
</persistence>

You might also like