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() +

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

for (int i = 0.java import java. "sa". while (rs.getColumnCount(). new String[] { "TABLE" }).persistence.out. } } } } } File: Main.createEntityManager(). javax.out. ResultSetMetaData metadata = rs.EntityManagerFactory. "").sql.out.println(" Column Type: "+ metadata.out.List. "). public class Main { static EntityManagerFactory emf = Persistence.persistence. javax.EntityManager.Query.println(" Column Name: "+ metadata. java.getColumnLab el(i + 1)+ ". java. i < metadata.sql. Statement st = conn. i++) { System. ResultSet rs = st. System.hsqldb.println(" Column Value: "+value+"\n"). import import import import javax. ResultSet mrs = conn.sql.util.getMetaData(). Object value = rs. System.ResultSet.getString(3). java. public class Helper { public static void checkData() throws Exception { Class.jdbcDriver").import import import import import java.println("\n\n\n\nTable Name: "+ tableName).next()) { System.Statement.sql. .createStatement().Connection.createEntityManagerFact ory("JPAService").begin().forName("org. public static void main(String[] a) throws Exception { em.getColumnTyp eName(i + 1)+ ": ").getTables(null. null.getConnection("jdbc:hsqldb:data/tut orial".persistence. System.getObject(i + 1). static EntityManager em = emf.next()) { String tableName = mrs. java. null.persistence.Persistence.out.getTransaction().getMetaData().println(" Row:"). Connection conn = DriverManager. Student student = new Student().ResultSetMetaData.DriverManager. while (mrs.sql.executeQuery("select * from " + tableName). javax.

username" value="sa"/> <property name="hibernate.0"> <persistence-unit name="JPAService" transactiontype="RESOURCE_LOCAL"> <properties> <property name="hibernate. emf.addStudent(student).url" value="jdbc:hsqldb:data /tutorial"/> </properties> </persistence-unit> </persistence> .commit().connection.com/xml/ns/persistence/persistence" version="1.close(). query = em.password" value=""/> <property name="hibernate.hsq ldb.xml <persistence xmlns="http://java.dialect.getResultList(). System.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java. dept.persist(student).sun. Department dept = new Department().out.connection.com/xml/ns/persistence http://java.getTransaction(). } } File: persistence.connection. List<Student> list = (List<Student>) query. System.hbm2ddl.flush().sun.createQuery("SELECT d FROM Department d").w3.println(dList).out.close().println(list).sun. em.setName("Joe").student. Helper.H SQLDialect"/> <property name="hibernate. em.persist(dept).connection.com/xml/ns/persistence" xmlns:xsi="http://www. em. em.auto" value="update"/> <property name="hibernate.dialect" value="org.hibernate.jdbcDriver"/> <property name="hibernate. List<Department> dList = (List<Department>) query.createQuery("SELECT e FROM Student e").driver_class" value="org. em.checkData().getResultList(). Query query = em.

Sign up to vote on this title
UsefulNot useful