2016-08-04 3 views
0

ユーザのリストをロケーションオブジェクトにマップしようとしていますが、マッピング例外が発生します。これは、Listオブジェクトがデータベースによって認識されないためですか?なぜこの例外が発生するのですか?オブジェクトのリストを春に休止状態のテーブルにマップするにはどうすればいいですか?

これは私のユーザークラスです:

@Entity 
@Table(name = "users") 
public class NewUser extends BaseEntity{ 
    private String login; 
    private String fullName; 

    private Location location; 
    private Department department; 
    private Role role; 
    private Long days; 
    private String team; 
    private Long managerId; 
    private String hiredDate; 

    public String getLogin() { 
     return login; 
    } 

    public void setLogin(String login) { 
     this.login = login; 
    } 

    public String getFullName() { 
     return fullName; 
    } 

    public void setFullName(String fullName) { 
     this.fullName = fullName; 
    } 

    public Location getLocation() { 
     return location; 
    } 

    @ManyToOne(targetEntity = Location.class) 
    @JoinTable(name = "location") 
    public void setLocation(Location location) { 
     this.location = location; 
    } 

    public Department getDepartment() { 
     return department; 
    } 

    @ManyToOne(targetEntity = Department.class) 
    public void setDepartment(Department department) { 
     this.department = department; 
    } 

    public Role getRole() { 
     return role; 
    } 

    @ManyToOne(targetEntity = Role.class) 
    @JoinTable(name = "role") 
    public void setRole(Role role) { 
     this.role = role; 
    } 

と場所クラス:

@Entity 
@Table(name = "location") 
public class Location extends BaseEntity{ 
    private List<NewUser> users; 

    public List<NewUser> getUsers() { 
     return users; 
    } 

    @OneToMany(targetEntity = NewUser.class, mappedBy = "location") 
    @JoinTable(name = "users") 
    public void setUsers(List<NewUser> users) { 
     this.users = users; 
    } 
} 

基本エンティティ:

@MappedSuperclass 
public abstract class BaseEntity { 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private long id; 

    public long getId() { 
     return id; 
    } 

    public void setId(long id) { 
     this.id = id; 
    } 
} 

エラーは次のとおりです。

Caused by: org.hibernate.MappingException: Could not determine type for: java.util.List, at table: location, for columns: [org.hibernate.mapping.Column(users)] 

hibernateアノテーションを使用してリレーショナルマッピングを効果的に作成するにはどうすればよいですか?

答えて

3

あなたのマッピングが正しくありません。対応する場所を指しますが、ユーザーテーブルでFKをしたいと仮定すると、次を使用します。

ユーザー

@ManyToOne 
@JoinColumn(name = "location_id") 
public void setLocation(Location location) { 
    this.location = location; 
} 

では場所では

@OneToMany(mappedBy = "location") 
public void setUsers(List<NewUser> users) { 
    this.users = users; 
} 

をあなたが場所への外部キーを望んでいなかった場合usersテーブルのuser_id(FK to users)とlocation_id(FK to locations)という列のjoinテーブル 'user_locations'を使用し、それに応じて@JoinTableアノテーションを使用することができます。あなたのマッピングでは、これは次のようになります:

@ManyToOne 
@JoinTable(name = "user_locations", joinColumns = @JoinColumn(name="user_id"), inverseJoinColumns = @JoinColumn(name = "location_id")) 
public void setLocation(Location location) { 
    this.location = location; 
} 
1

Hibernateは、@Tableアノテーションを使用して指定しているので、各エンティティのテーブルを既に知っています。あなたは2つの独立した関係の代わりに2つの独立した関係を確立しています。

@ManyToOne(fetch=FetchType.LAZY) 
@JoinColumn(name="id_user") 
public Location getLocation() { 
    return location; 
} 

も参照してください

Location.java

@OneToMany(fetch=FetchType.LAZY, cascade = {CascadeType.ALL}, mappedBy="location") 
public List<NewUser> getUsers() { 
    return users; 
} 

NewUser.javaを:mappedByを使用して

は、あなたがに頼るしたい他のentitiyのフィールドを宣言します。

+0

ありがとうございました@AlanHay、実際には関係を所有しているユーザーエンティティです。 –

関連する問題