Not able to get JPA 2 to work in EclipseLink [message #1018099] |
Wed, 13 March 2013 06:24 |
George Jackson Messages: 9 Registered: March 2013 |
Junior Member |
|
|
Dear EclipseLink Specialists,
I need your advice on getting a OneToMany (Employee to Photo) relational mapping using @ElementCollection & @CollectionTable in JPA 2.0. Below is a hypothetical code snippet that I am trying to get all the Photo records persisted that belongs to the same Employee without success:
@Entity
public class Employee implements Serializable
{
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String firstname;
private String surname;
private String address;
......
@ElementCollection(targetClass=Photo.class)
private Set<Photo> photos = new HashSet<>();
public Set<Photo> getPhotos()
{
return this.photos;
}
public void addPhotos(Photo photo)
{
if (!getPhotos().contains(photo))
{
getPhotos().add(photo);
}
}
public void addPhotos(Set<Photo> photos)
{
for (Photo photo: photos)
{
if (!getPhotos().contains(photo))
{
getPhotos().add(photo);
}
}
}
}
@Embeddable
public class Photo implements Serializable
{
public Photo() {}
public Photo(String name,String description,Date timeTaken)
{
this.name = name;
this.description = description;
this.timeTaken = timeTaken;
}
private String name;
private String description;
@Temporal(TemporalType.DATE)
private Date timeTaken;
.....
}
public class EmployeeService
{
protected EntityManager em;
public EmployeeService(EntityManager em)
{
this.em = em;
}
public Employee createEmployee(String firstname, String surname, String address,....)
{
Employee emp = new Employee();
emp.setFirstname(firstname);
emp.setSurname(surname);
emp.setAddress(address);
em.persist(emp);
return emp;
}
......
}
public class recordDailyActivities {
private static EntityManagerFactory emf =
Persistence.createEntityManagerFactory("JournalPU");
private static EntityManager em = emf.createEntityManager();
public static void main(String[] args) {
.........
Date today = new Date();
Employee employee = new Employee();
Photo beforeWorkPhoto = new Photo();
beforeWorkPhoto.name = "Morning exercise";
beforeWorkPhoto.description = "Workout at the gym";
beforeWorkPhoto.timeTaken = today;
Photo workPhoto = new Photo();
workPhoto.name = "At the Office";
workPhoto.description = "Hard at work";
workPhoto.timeTaken = today;
Photo afterWorkPhoto = new Photo();
afterWorkPhoto.name = "Catch up with friends";
afterWorkPhoto.description = "Dinning at the Club";
afterWorkPhoto.timeTaken = today;
employee.getPhotos().add(beforeWorkPhoto);
employee.getPhotos().add(workPhoto);
employee.getPhotos().add(afterWorkPhoto);
EmployeeService service = new EmployeeService(em);
em.getTransaction().begin();
Employee managedEmployee = service.createEmployee(employee);
em.getTransaction().commit();
.....
em.close();
emf.close();
}
}
Output of database structure:
Employee table
Column ID
Row 1
Employee_Photo
Column Employee_ID_Photo
Row 1
Column Photo
However, only the last photo (afterWorkPhoto) object has been persisted to the Employee entity instead of all 3 photos. I also want the option of going back to add more new (unique) photos for the same employee in the future.
I am running Eclipselink 2.4.1 (JPA 2.0), Java 7 on Windows XP & 7.
Your advice would be much appreciated.
Thanks in advance,
George
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02880 seconds