-->
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.  [ 1 post ] 
Author Message
 Post subject: MappedBy on field both id/foreign
PostPosted: Thu Jan 27, 2011 6:07 am 
Newbie

Joined: Thu Jan 27, 2011 6:00 am
Posts: 1
Hi,

I have three tables: units, units_authorities and authorities. The last one is irrelevant; units is a join table, so it has foreign keys for both units and authorities. It cointans additional columns.
Now what I want to do is to create a collection of units_authorities in units. I have tried the code below:

Code:
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GenerationType;
import javax.persistence.OneToMany;
import javax.persistence.GeneratedValue;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="units")
public class Unit{
   private Integer id;
   private List<UnitAuthority> unitAuthorities;
   
   @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="id")
   public Integer getId(){
      return id;
   }
   public void setId(Integer id) {
      this.id = id;
   }
   
   @OneToMany(mappedBy="unit" fetch=FetchType.EAGER)
   public List<UnitAuthority> getUnitAuthorities() {
      return unitAuthorities;
   }
   
   public void setUnitAuthorities(List<UnitAuthority> unitAuthorities) {
      this.unitAuthorities = unitAuthorities;
   }
}


Code:
import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity @Table(name="units_authorities")
public class UnitAuthority implements Serializable{
   private static final long serialVersionUID = 1L;
   private Unit unit;
   private String authority;
   private Integer usersNumber;
   
   @Id
   @OneToOne
   @JoinColumn(name="fk_units_id")
   public Unit getUnit() {
      return unit;
   }
   
   public void setUnit(Unit unit) {
      this.unit = unit;
   }
   
   @Id
   @Column(name="fk_dic_authorities_id")
   public String getAuthority() {
      return authority;
   }
   
   public void setAuthority(String authority) {
      this.authority = authority;
   }
   
   @Column(name="users_number")
   public Integer getUsersNumber() {
      return usersNumber;
   }
   
   public void setUsersNumber(Integer usersNumber) {
      this.usersNumber = usersNumber;
   }
}


But it gets me "mappedby reference an unknown target entity property" error. I already know that the problem is caused by @Id on unit in UnitAuthority. How to get around this?


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

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.