Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Not able to get JPA 2 to work in EclipseLink(Could not generate OneToMany records using @ElementCollection in JPA 2)
Not able to get JPA 2 to work in EclipseLink [message #1018099] Wed, 13 March 2013 06:24 Go to next message
George Jackson is currently offline George JacksonFriend
Messages: 6
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
Re: Not able to get JPA 2 to work in EclipseLink [message #1018808 is a reply to message #1018099] Thu, 14 March 2013 14:02 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1039
Registered: July 2009
Senior Member
Did you implement the equals method on Photo, and what do you get back if you call managedEmployee.getPhotos()?

Please try the latest EclipseLink build and see if you still get the problem, and if so file a bug. You might also try turning off weaving and change tracking to see if it plays a part in the problem as described here:
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Performance/Weaving/Disabling_Weaving_with_Persistence_Unit_Properties

Best Regards,
Chris
Re: Not able to get JPA 2 to work in EclipseLink [message #1020435 is a reply to message #1018808] Mon, 18 March 2013 06:58 Go to previous message
George Jackson is currently offline George JacksonFriend
Messages: 6
Registered: March 2013
Junior Member
Hi Chris,

Thanks for your feedback.

I have identified the caused from the actual code where only 1 instance of Photo was instantiated while over-settings new values to the same variables from a loop. An oversighted from my part.

All is well again. Much appreciated for your suggestion though.

Thanks,

George
Previous Topic:Is it possible to build EclipseLink 1.1.4 from source
Next Topic:Memory issue due to hard referenced entities
Goto Forum:
  


Current Time: Sat Dec 20 09:58:22 GMT 2014

Powered by FUDForum. Page generated in 0.01640 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software