0
我試圖在下面的JPA查詢加入,但出現以下錯誤:JPA查詢加入錯誤:org.hibernate.hql.internal.ast.QuerySyntaxException:路徑預期的加入
org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! [from com.crm.entity.User user join fetch Role role on role.user_id = user.id where user.deleted = false and user.enabled = true and user.username = :username]
這裏是執行:
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;
import org.springframework.stereotype.Repository;
import com.crm.entity.User;
@Transactional
@Repository
public class UserJpaDaoImpl implements UserJpaDaoCustom {
@PersistenceContext
private EntityManager em;
@Override
public User getUser(String username) {
Query query = em.createQuery("from User user "
+ "join fetch Role role on role.userId = user.id "
+ "where user.deleted = false "
+ "and user.enabled = true "
+ "and user.username = :username", User.class);
query.setParameter("username", username);
return (User)query.getSingleResult();
}
}
User
實體:
@Entity
@Table(name = "user")
public class User extends BaseEntity implements UserDetails, Visible {
private static final long serialVersionUID = 1L;
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
/* Spring Security fields*/
@OneToMany
@JoinColumn(name = "user_id")
private List<Role> roles;
...
Role
實體:
@Entity
@Table(name = "role")
public class Role implements GrantedAuthority, Identifiable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Integer id;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "user_id")
private Integer userId;
...
加入我的查詢時出了什麼問題?
JPQL查詢開始 「選擇{}別名」。其他任何內容都不符合JPA規範 –