0
我想通過使用彈簧數據JPA一對一關聯來連接兩個表並顯示其結果。下面我加入我的模型和信息庫類, 我的第一個模型類用戶是,彈簧數據jpa表加入一對一協會
@Entity
@Table(name = "users")
public class Users implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name = "username")
public String username;
@Column(name = "password")
public String password;
@Column(name = "privid")
public Integer privid;
@OneToOne()
@JoinColumn(name="join_privillage")
private Privillages priviJoin;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getPrivid() {
return privid;
}
public void setPrivid(Integer privid) {
this.privid = privid;
}
public Privillages getPriviJoin() {
return priviJoin;
}
public void setPriviJoin(Privillages priviJoin) {
this.priviJoin = priviJoin;
}
protected Users() {
}
public Users(String username, String password, Integer privid) {
this.username = username;
this.password = password;
this.privid = privid;
}
@Override
public String toString() {
return String.format("Users[id=%d, username='%s', password='%s']", id,
username, password);
}
}
而我的下一個模型類,我需要加入的是:
@Entity
@Table(name = "privillages")
public class Privillages implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer id;
@Column(name = "privid")
public Integer privid;
@Column(name = "privi_name")
public String privi_name;
@OneToOne(fetch=FetchType.LAZY, mappedBy="priviJoin")
public Users user;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getPrivid() {
return privid;
}
public void setPrivid(Integer privid) {
this.privid = privid;
}
public String getPrivi_name() {
return privi_name;
}
public void setPrivi_name(String privi_name) {
this.privi_name = privi_name;
}
public Users getUser() {
return user;
}
public void setUser(Users user) {
this.user = user;
}
public Privillages() {
}
public Privillages(Integer privid, String privi_name) {
this.privid = privid;
this.privi_name = privi_name;
}
}
而且我的倉庫是:
public interface UsersRepository extends CrudRepository<Users, Integer> {
@Query("SELECT u.username FROM Users u inner join p.privi_name FROM
Privillages p")
List<Users> findByUsername();
}
,當我運行的應用程序,它怎麼樣了錯誤像
「意外標記:FROM在第1行第74列[SELECT u.username FROM com.central.model.Users u inner join p.privi_name FROM com.central.model.Privillages p]」。
而且
「驗證失敗的查詢方法公共抽象 java.util.List的 com.central.repository.UsersRepository.findByUsername()!」。
我正在使用此查詢以id作爲條件。顯示用戶名和特權名稱。 「@ Query(」SELECT u.username,p.privi_name FROM Users u inner join Privillages p ON u.id = p.id「)」。但仍然顯示錯誤「加入的路徑預期! 」。 – Jacob
另外我的外鍵約束是放在兩個表上的privid。 – Jacob
現在嘗試更新 –