Пример вашего запроса
import java.util.HashMap;
import java.util.Map;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MapKey;
импорт javax.persistence.OneToMany;
@ Entity
общественный класс отдел {
@Id @GeneratedValue (стратегии = GenerationType.IDENTITY)
private int id;
личное строковое имя;
@OneToMany (mappedBy = "отдел")
@MapKey (имя = "имя")
личная карта студентов;
public Department() {
students = new HashMap<String, 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().containsKey(student.getName())) {
getStudents().put(student.getName(), student);
if (student.getDepartment() != null) {
student.getDepartment().getStudents().remove(student.getName());
}
student.setDepartment(this);
}
}
public Map<String,Student> getStudents() {
return students;
}
public String toString() {
return "Department id: " + getId() +
", name: " + getName();
}
}
Файл: Student.java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@ Entity
публичный класс Student {
@Id @GeneratedValue (стратегии = GenerationType.IDENTITY)
private int id;
личное строковое имя;
@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();
}
}
Файл: Helper.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
Публичный класс Хелпер {
public static void checkData () выдает Exception {
Class.forName ( "org.hsqldb.jdbcDriver");
Соединение conn = DriverManager.getConnection ("jdbc: hsqldb: data / tutorial", "sa", "");
Оператор 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.getColumnLabel(i + 1)+ ", ");
System.out.println(" Column Type: "+ metadata.getColumnTypeName(i + 1)+ ": ");
Object value = rs.getObject(i + 1);
System.out.println(" Column Value: "+value+"\n");
}
}
}
}
}
Файл: Main.java
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
импорт javax.persistence.Persistence;
импорт javax.persistence.Query;
публичный класс Main {
static EntityManagerFactory emf = Persistence.createEntityManagerFactory ("JPAService");
static EntityManager em = emf.createEntityManager ();
public static void main (String [] a) создает исключение {
em.getTransaction () начинается ();.
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();
}
}
Файл: persistence.xml
http://java.sun.com/xml/ns/persistence/persistence" version = "1.0">