我正在使用spring-data-jpa。我写了一个原生查询,但它不起作用。这是我的实体类:
@Entity
@Table(name="view_version")
public class ViewVersionDom {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name="view_id")
private ViewDom view;
private Integer version;
@ManyToOne
@JoinColumn(name="datasource_param_id")
private DatasourceParamDom datasourceParam;
private String description;
@Column(name="created_date")
private Date createdDate;
@Entity
@Table(name="view_permission")
public class ViewPermissionDom extends BaseDom {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name="view_id")
private ViewDom view;
@ManyToOne
@JoinColumn(name="user_id")
private UserDom user;
@ManyToOne
@JoinColumn(name="group_id")
private GroupDom group;
private Boolean read;
private Boolean write;
private Boolean execute;
以下是查询:
@Query(value = " SELECT v FROM ViewVersionDom v LEFT JOIN ViewPermissionDom vp ON v.view.id = vp.id "
+ " where (v.view.user.id = ?1 OR (vp.read=true and (vp.user.id=?1 or vp.user.id is NULL and vp.group.id is NULL or vp.group.id in (?2)))) "
+ " ORDER BY v.view.name", nativeQuery=true)
public List<ViewVersionDom> findUserViews(Long userId, List<Long> groupIds);
首先,当我没有写nativeQuery = true时,应用程序没有构建,我得到了一个异常'加入jpa的路径'。当我设置nativeQuery = true设置时,应用程序启动,但是当我调用该函数时,我收到以下错误:
org.hibernate.engine.jdbc.spi.SqlExceptionHelper - [ERROR: relation "viewversiondom" does not exist Position: 16]
org.hibernate.exception.SQLGrammarException: could not extract ResultSet]
是否有其他设置或注释可以解决问题? 我在谷歌搜索,但在所有情况下,2个表直接相互连接。
答案 0 :(得分:1)
您的查询不是SQL查询(假设您的表中没有列v
)。
表viewversiondom
也不存在,或者用于连接的数据库用户无法访问。
此外,在将原始查询映射到域对象时,您应该查看https://jira.spring.io/browse/DATAJPA-980