|
|
Re: Is TABLE_PER_CLASS supposed to work with eclipselink-2.0.0.v20090731-r4765 (m6) [message #482163 is a reply to message #481849] |
Tue, 25 August 2009 14:50 |
No real name Messages: 5 Registered: August 2009 |
Junior Member |
|
|
For my example I'm using the following two entity classes Student and Department:
import java.util.*;
import javax.persistence.*;
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Student {
@Id
@GeneratedValue(strategy=GenerationType.TABLE)
private int id;
private String name;
@ManyToMany
private Collection<Department> departments;
public Student() {
departments = new ArrayList<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 void addDepartment(Department department) {
if (!getDepartments().contains(department)) {
getDepartments().add(department);
}
if (!department.getStudents().contains(this)) {
department.getStudents().add(this);
}
}
public Collection<Department> getDepartments() {
return departments;
}
public void setDepartment(Collection<Department> departments) {
this.departments = departments;
}
public String toString() {
return "Student id: " + getId() + ", name: " + getName();
}
}
import java.util.*;
import javax.persistence.*;
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Department {
@Id
@GeneratedValue(strategy=GenerationType.TABLE)
private int id;
private String name;
@ManyToMany(mappedBy="departments")
private Collection<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.getDepartments().contains(this)) {
student.getDepartments().add(this);
}
}
public Collection<Student> getStudents() {
return students;
}
public void setStudent(Collection<Student> students) {
this.students = students;
}
public String toString() {
return "Department id: " + getId() + ", name: " + getName();
}
}
I have a simple Main class which just does:
import java.util.*;
import javax.persistence.*;
public class Main {
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.println("No provider (hibernate|eclipselink) specified.");
System.exit(1);
}
String provider = args[0];
EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAService_"+provider);
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Student student1 = new Student();
em.persist(student1);
student1.setName("Joe");
Student student2 = new Student();
em.persist(student2);
student2.setName("Tom");
Department dept = new Department();
em.persist(dept);
dept.setName("MyDepartment");
dept.addStudent(student1);
dept.addStudent(student2);
em.flush();
em.getTransaction().commit();
System.out.println("Students:");
Query query = em.createQuery("SELECT e FROM Student e");
List<Student> students = (List<Student>) query.getResultList();
for (Student student : students) {
System.out.println(student);
}
System.out.println("Departments:");
query = em.createQuery("SELECT d FROM Department d");
List<Department> departments = (List<Department>) query.getResultList();
for (Department department : departments) {
System.out.println(department);
}
em.close();
emf.close();
}
}
I'm using the following persistence.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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_1_0.xsd">
<persistence-unit name="JPAService_hibernate" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>Department</class>
<class>Student</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.connection.driver_class" value="org.hsqldb.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-unit name="JPAService_eclipselink" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>Department</class>
<class>Student</class>
<properties>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="database"/>
<property name="eclipselink.target-server" value="none"/>
<property name="eclipselink.target-database" value="HSQL"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:data/tutorial"/>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
<property name="eclipselink.logging.logger" value="JavaLogger"/>
<property name="eclipselink.logging.level" value="FINEST"/>
</properties>
</persistence-unit>
</persistence>
When using hibernate as a persistence provider everything works as expected. When using eclipselink the code throws exceptions as shown in my first message. I would be very happy if someone could tell me what's wrong with the code.
Best regards,
Martin
[Updated on: Tue, 25 August 2009 15:25] Report message to a moderator
|
|
|
|
|
Re: Is TABLE PER CLASS supposed to work with eclipselink-2.0.0.v20090731-r4765 (m6) [message #483485 is a reply to message #482477] |
Tue, 01 September 2009 17:38 |
|
Please include your source files for your Entity classes which include the
annotations you are using, or include the XML if you are using the orm.xml
to define the mappings.
Just past them into your post, I probably don't need your entire source
file, but at a minimum include the annotations that you specified and
their values, otherwise I have no way to verify or recreate your issue.
It seems the column names you are giving are wrong, or their is a bug in
the auto mapping code if you have not set any.
James : Wiki : Book : Blog : Twitter
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04203 seconds