0% found this document useful (0 votes)
72 views4 pages

C C C C C C C C C: "Department" "Name ASC"

This document contains Java code defining entities for a Department and Student using JPA annotations. It also includes code to persist these entities to an HSQLDB database using JPA and retrieve them. Helper code is provided to check and print the database contents.

Uploaded by

Maha Kharbech
Copyright
© Attribution Non-Commercial (BY-NC)
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)
72 views4 pages

C C C C C C C C C: "Department" "Name ASC"

This document contains Java code defining entities for a Department and Student using JPA annotations. It also includes code to persist these entities to an HSQLDB database using JPA and retrieve them. Helper code is provided to check and print the database contents.

Uploaded by

Maha Kharbech
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 4

import java.util.ArrayList; import java.util.Collection; import java.util.List; import import import import import import javax.persistence.Entity; javax.persistence.GeneratedValue; javax.persistence.

GenerationType; javax.persistence.Id; javax.persistence.OneToMany; javax.persistence.OrderBy;

@Entity public class Department { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; @OneToMany(mappedBy="department") @OrderBy("name ASC") private List<Student> students; public Department() { students = new ArrayList<Student>(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String deptName) { this.name = deptName; } public void addStudent(Student student) { if (!getStudents().contains(student)) { getStudents().add(student); if (student.getDepartment() != null) { student.getDepartment().getStudents().remove(student); } student.setDepartment(this); } } public Collection<Student> getStudents() { return students; } public String toString() { return "Department id: " + getId() +

", name: " + getName(); } }

File: Student.java
import import import import import javax.persistence.Entity; javax.persistence.GeneratedValue; javax.persistence.GenerationType; javax.persistence.Id; javax.persistence.ManyToOne;

@Entity public class Student { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; @ManyToOne private Department department; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } public String toString() { return "Student id: " + getId() + " name: " + getName() + " with " + getDepartment(); } }

File: Helper.java

import import import import import

java.sql.Connection; java.sql.DriverManager; java.sql.ResultSet; java.sql.ResultSetMetaData; java.sql.Statement;

public class Helper { public static void checkData() throws Exception { Class.forName("org.hsqldb.jdbcDriver"); Connection conn = DriverManager.getConnection("jdbc:hsqldb:data/tut orial", "sa", ""); Statement st = conn.createStatement(); ResultSet mrs = conn.getMetaData().getTables(null, null, null, new String[] { "TABLE" }); while (mrs.next()) { String tableName = mrs.getString(3); System.out.println("\n\n\n\nTable Name: "+ tableName); ResultSet rs = st.executeQuery("select * from " + tableName); ResultSetMetaData metadata = rs.getMetaData(); while (rs.next()) { System.out.println(" Row:"); for (int i = 0; i < metadata.getColumnCount(); i++) { System.out.println(" Column Name: "+ metadata.getColumnLab el(i + 1)+ ", "); System.out.println(" Column Type: "+ metadata.getColumnTyp eName(i + 1)+ ": "); Object value = rs.getObject(i + 1); System.out.println(" Column Value: "+value+"\n"); } } } } }

File: Main.java
import java.util.List; import import import import javax.persistence.EntityManager; javax.persistence.EntityManagerFactory; javax.persistence.Persistence; javax.persistence.Query;

public class Main { static EntityManagerFactory emf = Persistence.createEntityManagerFact ory("JPAService"); static EntityManager em = emf.createEntityManager(); public static void main(String[] a) throws Exception { em.getTransaction().begin(); Student student = new Student();

student.setName("Joe"); em.persist(student); Department dept = new Department(); dept.addStudent(student); em.persist(dept); em.flush();

Query query = em.createQuery("SELECT e FROM Student e"); List<Student> list = (List<Student>) query.getResultList(); System.out.println(list); query = em.createQuery("SELECT d FROM Department d"); List<Department> dList = (List<Department>) query.getResultList(); System.out.println(dList); em.getTransaction().commit(); em.close(); emf.close(); Helper.checkData(); } }

File: persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence" version="1.0"> <persistence-unit name="JPAService" transactiontype="RESOURCE_LOCAL"> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.H SQLDialect"/> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.connection.driver_class" value="org.hsq ldb.jdbcDriver"/> <property name="hibernate.connection.username" value="sa"/> <property name="hibernate.connection.password" value=""/> <property name="hibernate.connection.url" value="jdbc:hsqldb:data /tutorial"/> </properties> </persistence-unit> </persistence>

You might also like