jpql连接查询

发布于 2024-11-16 05:03:27 字数 3286 浏览 4 评论 0原文

我有一个名为 MenuPrevilege 的关联表,位于两个名为 Menu 和 Previlege 的表之间。 为了获取特定权限的所有菜单,我在 Menu 实体中创建了一个命名查询:

   @Entity
   @NamedQueries( {
   @NamedQuery(name = "getAllMenus", query = "select m from Menu m"),
   @NamedQuery(name = "getMenusByPrevilegeId", query = "select m from Menu m
            JOIN m.menuPrevilege mp where mp.previlege_id = :p")})


public class Menu implements Serializable {

private String url;
private String description;
private List<MenuPrevilege> menuPrevilges;
private static final long serialVersionUID = 1L;

public Menu() {
    super();
}

@Id
public String getUrl() {
    return this.url;
}

public void setUrl(String url) {
    this.url = url;
}

public String getDescription() {
    return this.description;
}

public void setDescription(String description) {
    this.description = description;
}

public void setMenuPrevilges(List<MenuPrevilege> menuPrevilges) {
    if (menuPrevilges == null)
        menuPrevilges = new ArrayList<MenuPrevilege>();
    this.menuPrevilges = menuPrevilges;
}

@OneToMany(mappedBy = "menu", cascade = CascadeType.REMOVE)
public List<MenuPrevilege> getMenuPrevilges() {
    if (menuPrevilges == null)
        menuPrevilges = new ArrayList<MenuPrevilege>();
    return menuPrevilges;
}

public Menu(String url, String description) {
    super();
    this.url = url;
    this.description = description;
}
    }

我遇到了此异常 org.hibernate.QueryException: 无法解析 property:menuPrevilege ,并且我不知道如何处理它。这是 MenuPrevilege 实体:

    @Entity
    @Table(name = "Menu_Previlege")

    public class MenuPrevilege implements Serializable {

private IdMenuPrevilege idmenuPrevilege = new IdMenuPrevilege();
private Date activationDate;
private Date deactivationDate;
private Menu menu;
private Previlege previlege;
private static final long serialVersionUID = 1L;

public MenuPrevilege() {
    super();
}

@EmbeddedId
public IdMenuPrevilege getIdmenuPrevilege() {
    return this.idmenuPrevilege;
}

public void setIdmenuPrevilege(IdMenuPrevilege idmenuPrevilege) {
    this.idmenuPrevilege = idmenuPrevilege;
}

@Temporal(TemporalType.DATE)
public Date getActivationDate() {
    return this.activationDate;
}

public void setActivationDate(Date activationDate) {
    this.activationDate = activationDate;
}

@Temporal(TemporalType.DATE)
public Date getDeactivationDate() {
    return this.deactivationDate;
}

public void setDeactivationDate(Date deactivationDate) {
    this.deactivationDate = deactivationDate;
}

public void setMenu(Menu menu) {
    this.menu = menu;
}

@ManyToOne
@JoinColumn(name = "menu_id", insertable = false, updatable = false)
public Menu getMenu() {
    return menu;
}

public void setPrevilege(Previlege previlege) {
    this.previlege = previlege;
}

@ManyToOne
@JoinColumn(name = "previlege_id", insertable = false, updatable = false)
public Previlege getPrevilege() {
    return previlege;
}

public MenuPrevilege(Menu menu, Previlege previlege) {
    super();
    getIdmenuPrevilege().setIdMenu(menu.getUrl());
    getIdmenuPrevilege().setIdPrevilege(previlege.getPrevilegeId());
    this.setMenu(menu);
    this.setPrevilege(previlege);
    menu.getMenuPrevilges().add(this);
    previlege.getPrevilegeMenus().add(this);
}

  }

i have an association table called MenuPrevilege between 2 tables called Menu and Previlege.
In order to get all menus of a specific previlege i created a named query in the Menu entity:

   @Entity
   @NamedQueries( {
   @NamedQuery(name = "getAllMenus", query = "select m from Menu m"),
   @NamedQuery(name = "getMenusByPrevilegeId", query = "select m from Menu m
            JOIN m.menuPrevilege mp where mp.previlege_id = :p")})


public class Menu implements Serializable {

private String url;
private String description;
private List<MenuPrevilege> menuPrevilges;
private static final long serialVersionUID = 1L;

public Menu() {
    super();
}

@Id
public String getUrl() {
    return this.url;
}

public void setUrl(String url) {
    this.url = url;
}

public String getDescription() {
    return this.description;
}

public void setDescription(String description) {
    this.description = description;
}

public void setMenuPrevilges(List<MenuPrevilege> menuPrevilges) {
    if (menuPrevilges == null)
        menuPrevilges = new ArrayList<MenuPrevilege>();
    this.menuPrevilges = menuPrevilges;
}

@OneToMany(mappedBy = "menu", cascade = CascadeType.REMOVE)
public List<MenuPrevilege> getMenuPrevilges() {
    if (menuPrevilges == null)
        menuPrevilges = new ArrayList<MenuPrevilege>();
    return menuPrevilges;
}

public Menu(String url, String description) {
    super();
    this.url = url;
    this.description = description;
}
    }

i'm having this exception org.hibernate.QueryException: could not resolve property:menuPrevilege , and i don't know how to deal with it. this is the MenuPrevilege entity:

    @Entity
    @Table(name = "Menu_Previlege")

    public class MenuPrevilege implements Serializable {

private IdMenuPrevilege idmenuPrevilege = new IdMenuPrevilege();
private Date activationDate;
private Date deactivationDate;
private Menu menu;
private Previlege previlege;
private static final long serialVersionUID = 1L;

public MenuPrevilege() {
    super();
}

@EmbeddedId
public IdMenuPrevilege getIdmenuPrevilege() {
    return this.idmenuPrevilege;
}

public void setIdmenuPrevilege(IdMenuPrevilege idmenuPrevilege) {
    this.idmenuPrevilege = idmenuPrevilege;
}

@Temporal(TemporalType.DATE)
public Date getActivationDate() {
    return this.activationDate;
}

public void setActivationDate(Date activationDate) {
    this.activationDate = activationDate;
}

@Temporal(TemporalType.DATE)
public Date getDeactivationDate() {
    return this.deactivationDate;
}

public void setDeactivationDate(Date deactivationDate) {
    this.deactivationDate = deactivationDate;
}

public void setMenu(Menu menu) {
    this.menu = menu;
}

@ManyToOne
@JoinColumn(name = "menu_id", insertable = false, updatable = false)
public Menu getMenu() {
    return menu;
}

public void setPrevilege(Previlege previlege) {
    this.previlege = previlege;
}

@ManyToOne
@JoinColumn(name = "previlege_id", insertable = false, updatable = false)
public Previlege getPrevilege() {
    return previlege;
}

public MenuPrevilege(Menu menu, Previlege previlege) {
    super();
    getIdmenuPrevilege().setIdMenu(menu.getUrl());
    getIdmenuPrevilege().setIdPrevilege(previlege.getPrevilegeId());
    this.setMenu(menu);
    this.setPrevilege(previlege);
    menu.getMenuPrevilges().add(this);
    previlege.getPrevilegeMenus().add(this);
}

  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心房敞 2024-11-23 05:03:27

我对我的代码进行了名称重构,编辑了我的查询,一切似乎都正常。以下是更改:

  1. 在命名查询中:

    @NamedQuery(name = "getMenusByPrevilegeId", query = "从菜单中选择 m JOIN 
     m.previleges p 其中 p.previlege.previlegeId = :p")})
    
  2. 实体属性

    私有列表特权;
    // getter 和 setter 也是如此
    
  3. 在MenuPrevilege实体的构造函数中

    public MenuPrevilege(菜单菜单,特权特权) {
    极好的();
    getIdmenuPrevilege().setIdMenu(menu.getUrl());
    getIdmenuPrevilege().setIdPrevilege(previlege.getPrevilegeId());
    this.setMenu(菜单);
    this.setPrevilege(特权);
    menu.getPrevileges().add(this);
    previlege.getMenus().add(this);
    }
    

,您可以注意到这是我的查询中的语法错误导致了异常。

I made name refactoring to my code edit my query and everything seems to be working. Here are the changes :

  1. in the named query:

    @NamedQuery(name = "getMenusByPrevilegeId", query = "select m from Menu m  JOIN 
     m.previleges p where p.previlege.previlegeId = :p")})
    
  2. the entity attribute

    private List<MenuPrevilege> previleges;
    // getters and setters as well
    
  3. in the constructor of the MenuPrevilege entity

    public MenuPrevilege(Menu menu, Previlege previlege) {
    super();
    getIdmenuPrevilege().setIdMenu(menu.getUrl());
    getIdmenuPrevilege().setIdPrevilege(previlege.getPrevilegeId());
    this.setMenu(menu);
    this.setPrevilege(previlege);
    menu.getPrevileges().add(this);
    previlege.getMenus().add(this);
    }
    

as u can notice it was a syntax error in my query that caused the exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文