-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Many to Many Association with additional column
PostPosted: Fri Sep 24, 2010 10:30 pm 
Newbie

Joined: Wed Jun 18, 2008 9:17 am
Posts: 12
Hi All,
I am learning Hibernate and currently stuck and need some help to figure out what I have done wrong.

Tables:
employee(
emp_id,firstname,lastname
)

project(
proj_id, name
)

empproj(
projectid,employeeid, projectlead
)

Emplyee.java
Code:
@Entity
@Table(name="employee")
public class Employee implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="emp_id")
    private Integer employeeId;
    private String firstName;
    private String lastName;
    @OneToMany(mappedBy = "pk.project",fetch=FetchType.LAZY,cascade=CascadeType.ALL)
    private List<ProjectAssociation> projects = new ArrayList<ProjectAssociation>();
    ...
   }


Project class
Code:
@Entity
@Table(name="project")
public class Project implements Serializable{

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="proj_id")
    private Integer projectId;
    @Column(name="name")
    private String  projectName;
    @OneToMany(mappedBy = "pk.project",fetch=FetchType.LAZY)
    private List<ProjectAssociation> employees =new ArrayList<ProjectAssociation>();
    ...
    }


ProjectAssociation with additional attribute
Code:
@Entity(name="projemp")
@AssociationOverrides({
@AssociationOverride(name = "pk.employee", joinColumns = @JoinColumn(name = "employeeId")),
@AssociationOverride(name = "pk.project", joinColumns = @JoinColumn(name = "projectId"))})

public class ProjectAssociation implements Serializable {
    @EmbeddedId
    private  ProjectAssociationId  pk = new ProjectAssociationId();
   
    @Transient
    @ManyToOne(optional=false)
    private Employee employee;
    @Transient
    @ManyToOne(optional=false, fetch=FetchType.EAGER)
    private Project project;
    @Basic
    private boolean projectLead;
    ...
    }


ProjectAssociationId

Code:
@Embeddable
public class ProjectAssociationId implements Serializable {
    @ManyToOne
    private Employee employee;
    @ManyToOne
    private Project project;



My current issues

- insert operation works fine as expected
- Select not working correctly

ProjectAssociation is always empty, The database has correct links between employee, progemp, and project.
Sql generated when emp.getProjects() is called is :

Code:
select projects0_.projectId as projectId1_, projects0_.employeeId as employeeId1_, projects0_.employeeId as employeeId4_0_, projects0_.projectId as projectId4_0_, projects0_.projectLead as projectL1_4_0_ from projemp projects0_ where projects0_.projectId=?


What am I doing wrong, When I call emp.getProjects() I want to get all the projects this employee is working on

Code:
Employee emp = em.find(Employee.class, 20);
        System.out.println("emp= " + ReflectionToStringBuilder.toString(emp));
        List<ProjectAssociation> projs = emp.getProjects();
        for(ProjectAssociation pj: projs){
           Project p = pj.getProject();
           System.out.println("proj = " + ReflectionToStringBuilder.toString(p));
        }


Top
 Profile  
 
 Post subject: Re: Many to Many Association with additional column
PostPosted: Sat Sep 25, 2010 5:41 am 
Newbie

Joined: Wed Jun 18, 2008 9:17 am
Posts: 12
i figured it out. I was using the wrong method

I should be using the pk property to access project/employee.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.