-->
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: Mapping multiple composite @ManyToOne
PostPosted: Tue May 31, 2011 3:32 am 
Newbie

Joined: Wed May 04, 2011 2:24 am
Posts: 10
Hello everyone,

I'm having some trouble annotating entities for a certain type of datamodel. The datamodel looks as following:

Image

Customer has a composite PK based on an FK with Country and a customer_id.
Department has a composite PK based on an FK with Country and a project_id.
Project has an FK relationship with Country, Department and Customer.

Now when @ManyToOne relationships are mapped, we'll get three of them in the Project entity, for Country, Customer and Department. However, all three of these are based on the underlying country_id field.

The Project entity will look a bit like this:

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

   private static final long serialVersionUID = 646965562304786L;

   @Id
   @Column(name = "Project_id", nullable = false)
    String projectId;

   public String getProjectId() {
      return projectId;   
   }

   public void setProjectId(String projectId) {
      this.projectId = projectId;
   }

   @Column(name = "Description", nullable = true)
    String description;

   public String getDescription() {
      return description;   
   }

   public void setDescription(String description) {
      this.description = description;
   }

   @ManyToOne
    @JoinColumns(value = {
      @JoinColumn(name = "Country_id", referencedColumnName = "Country_id"),
   })
   private Country country;
   public Country getCountry() { return country; }
   public void setCountry(Country country) { this.country = country; }

   @ManyToOne
    @JoinColumns(value = {
      @JoinColumn(name = "Country_id", referencedColumnName = "Country_id"),
      @JoinColumn(name = "Customer_id", referencedColumnName = "Customer_id"),
   })
   private Customer customer;
   public Customer getCustomer() { return customer; }
   public void setCustomer(Customer customer) { this.customer = customer; }

   @ManyToOne
    @JoinColumns(value = {
      @JoinColumn(name = "Country_id", referencedColumnName = "Country_id"),
      @JoinColumn(name = "Department_id", referencedColumnName = "Department_id"),
   })
   private Department department;
   public Department getDepartment(){ return department; }
   public void setDepartment(Department department) { this.department = department; }
   
}


Hibernate doesn't like this, because the column Country_id will be mapped three times in the Project entity:
org.hibernate.MappingException: Repeated column in mapping for entity: hibernateentities.entities.Project column: Country_id (should be mapped with insert="false" update="false")

I changed the @JoinColumns to the following in order to prevent the repeated mappings.
Code:
    @ManyToOne
    @JoinColumns(value = {
      @JoinColumn(name = "Country_id", referencedColumnName = "Country_id", insertable = false, updatable = false),
      @JoinColumn(name = "Department_id", referencedColumnName = "Department_id"),
   })

My theory was that I'd set the Country entity in the setter method based on the provided Department. However, unfortunately it gave another exception:
org.hibernate.AnnotationException: : Mixing insertable and non insertable columns in a property is not allowed: hibernateentities.entities.Projectdepartment

How can I map this properly? Thank you for your time.

Edit: Found a topic with a simlar problem, but based around XML declaration instead of JPA annotations.


Top
 Profile  
 
 Post subject: Re: Mapping multiple composite @ManyToOne
PostPosted: Wed Jul 06, 2011 5:03 am 
Newbie

Joined: Wed May 04, 2011 2:24 am
Posts: 10
I have still not found a solution for this problem. The data structure is not uncommon, has anyone ever found a solution for a similar problem?


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.