在使用RIGHT获得异常时

时间:2016-03-12 11:07:44

标签: java database hibernate

我的查询如下:

String query = "String query = "SELECT t1.temp_id, (RIGHT (t1.temp_id, CHARINDEX( ':', REVERSE(t1.temp_id)) -1)), t2.value, t3.value "   
                       +"FROM table1 t1 LEFT JOIN table2 t2 " 
                       + "ON CAST(LEFT(t1.temp_id,CHARINDEX(':',t1.temp_id)-1) AS INT)=t2.ID "
                       + "LEFT JOIN table3 t3 ON CAST(RIGHT(t1.temp_id,CHARINDEX(':',REVERSE(t1.temp_id))-1) AS INT)=t3.ID ";

创建查询时获取异常;

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: RIGHT near line 1, column 56 

可以从此article引用查询的拆分部分。

1 个答案:

答案 0 :(得分:0)

在查看this查询后,您似乎希望在hibernate中实现“多对多”功能。

即。像用户角色映射

一个用户可能有多个角色,一个角色可能有很多用户

'@im': /<img /

用户类

@Entity
public class Role
{
    @Id @GeneratedValue
    private int id;
    private String authority;

    @ManyToMany(mappedBy="roles")
    Collection<User> users=new ArrayList();

    public Collection<User> getUsers() {
        return users;
    }
    public void setUsers(Collection<User> users) {
        this.users = users;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getAuthority() {
        return authority;
    }
    public void setAuthority(String authority) {
        this.authority = authority;
    }
}

User and Role table schema

Hibernate实现:

@Entity
public class User
{
    @Id @GeneratedValue
    private int id;
    private String username;

    @ManyToMany
    Collection<Role> roles=new ArrayList();

    public Collection<Role> getRoles() {
        return roles;
    }
    public void setRoles(Collection<Role> roles) {
        this.roles = roles;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
}