spring one data jpa tables by one to one Association

时间:2017-10-04 11:49:11

标签: java spring jpa spring-data-jpa

我正在尝试连接两个表并通过使用spring数据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);
}
}

我需要加入的下一个Model类是:

@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();
}

当我运行应用程序时,它会出现像

这样的错误
  

&#34;意外令牌:FROM第1行第74列[SELECT u.username FROM   com.central.model.Users u inner join p.privi_name FROM   com.central.model.Privillages p]&#34;。

并且

  

&#34;查询方法公共摘要的验证失败   java.util.List中   com.central.repository.UsersRepository.findByUsername()&#34;!

1 个答案:

答案 0 :(得分:2)

  • 您正在查询中使用投影,但期望整个实体成为结果。
  • 您也是以错误的方式加入并使用数据库列名而不是映射的字段名。
  • 您正在使用两次..

如果您希望将整个实体作为结果并且Privilege已与User一起提取,请尝试:

@Query("SELECT u FROM Users u inner join fetch u.priviJoin")

<强>更新

如果您想使用投影,则必须更改结果类型:

@Query("SELECT u.username,p.privi_name FROM Users u inner join u.priviJoin p")
List<Object[]> findByUsername();

在这种情况下你也不需要fetch