2017-02-21 90 views
0

當我試圖獲得一對多和多對一的關係時,我遇到了車輛表上的連接列的問題。爲什麼hibernate正在搜索列USER_ID而不是將它加入到表中?我知道加入的目的,但是當我嘗試將它與映射的By屬性一起使用時,我得到了這個錯誤。該代碼有什麼問題?Hibernate mappedBy和@JoinColumn

例外,我得到:

產生的原因:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:未知列 'USER_ID' 在 '字段列表'

用戶表:

@Entity 
@Table(name = "users") 
public class User { 

public User(){} 

public int getId() { 
    return id; 
} 

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

public String getFirst_name() { 
    return first_name; 
} 

public void setFirst_name(String first_name) { 
    this.first_name = first_name; 
} 

public int getSalary() { 
    return salary; 
} 

public void setSalary(int salary) { 
    this.salary = salary; 
} 

public String getLast_name() { 
    return last_name; 
} 

public void setLast_name(String last_name) { 
    this.last_name = last_name; 
} 

@Override 
public String toString() { 
    return "User{" + 
      "id=" + id + 
      ", first_name='" + first_name + '\'' + 
      ", salary=" + salary + 
      ", last_name=" + last_name + 
      '}'; 
} 

public Collection<Vehicle> getVehicles() { 
    return vehicles; 
} 

public void setVehicles(Collection<Vehicle> vehicles) { 
    this.vehicles = vehicles; 
} 

@OneToMany(mappedBy = "user") 
private Collection<Vehicle> vehicles = new ArrayList<>(); 

public Collection<Address> getAddresses() { 
    return addresses; 
} 
public void setAddresses(Collection<Address> addresses) { 
    this.addresses = addresses; 
} 

    @ElementCollection // for saving the collection or persist it to database 
    @JoinTable(joinColumns = @JoinColumn(name = "fk_user_id")) // will add to the fk created table renamed column "fk_user_id" 
    private Collection<Address> addresses = new ArrayList<>(); 

    @Id 
    @Column(name = "user_id") 
    private int id; 
    @Column(name = "first_name") 
    private String first_name; 
    @Column(name = "salary") 
    private int salary; 
    @Column(name = "last_name") 
    private String last_name; 
} 

車輛:

@Entity 
@Table(name = "vehicle") 
public class Vehicle { 

public User getUser() { 
    return user; 
} 

public void setUser(User user) { 
    this.user = user; 
} 

@ManyToOne 
@JoinColumn(name = "USER_ID") 
private User user; 

public int getVehicle_id() { 
    return vehicle_id; 
} 

public void setVehicle_id(int vehicle_id) { 
    this.vehicle_id = vehicle_id; 
} 

public String getVehicle_name() { 
    return vehicle_name; 
} 

public void setVehicle_name(String vehicle_name) { 
    this.vehicle_name = vehicle_name; 
} 

@Id 
private int vehicle_id; 
private String vehicle_name; 

} 
+0

你的Vehicle表有USER_ID列嗎? –

+0

不! vehicle_id和vehicle_name,@Join列基本上是重命名存在列? –

+0

據我瞭解,它可以將列連接到一個表,並可以重命名它..我有我的用戶表中的user_id列; –

回答

1

你對待@JoinColumn有點不同,你認爲。

當你有一個配置:

public class Vehicle { 

    @ManyToOne 
    @JoinColumn(name = "USER_ID") 
    private User user; 

它告訴持久性提供使用VEHICLE.USER_ID列被加入VEHICLEUSERS表時。

默認情況下,在USERS表側,主鍵被使用,在這種情況下是具有相同名稱的列:USERS.USER_ID(除非你明確指定referencedColumnName財產我的@JoinColumn)。

底線是你需要在你的VEHICLE表中有一個user_id列。