注释ManyToMany关系:错误:“字段列表”中的未知列

时间:2015-03-08 10:40:55

标签: java hibernate

我已经看过一些关于如何注释ManyToMany关系的不同教程。

我在尝试返回用户区域列表时遇到的错误是:错误:未知列' areadao1_.idArea'在'字段列表'

问题类似于这篇文章:Hibernate: Unknown column in field list,但帖子没有给我任何答案。

模特: enter image description here

所以我有区域和用户。每个区域都可以拥有用户,或者每个用户都可以拥有区域。映射表和列的命名由mysql workbench自动生成。

以下是我的注释类的样子:
AreaDAO.java:

@Entity
@Table(name= "area")
public class AreaDAO {

    private int idArea;
    private String areaName;
    private AreaDAO parent;

    private Set<UserDAO> users = new HashSet<UserDAO>();

    @Id
    @Column(name="idArea")
    @GeneratedValue
    public int getIdArea() {
        return idArea;
    }
    public void setIdArea(int idArea) {
        this.idArea = idArea;
    }

    @Column(name="areaName")
    public String getAreaName() {
        return areaName;
    }
    public void setAreaName(String areaName) {
        this.areaName = areaName;
    }

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="parent", referencedColumnName="idArea")
    public AreaDAO getParent() {
        return parent;
    }
    public void setParent(AreaDAO parent) {
        this.parent = parent;
    }

    @ManyToMany
    @JoinTable(name="area_has_user", 
                joinColumns={@JoinColumn(name="area_idAarea", referencedColumnName="idArea")}, 
                inverseJoinColumns={@JoinColumn(name="user_idUser", referencedColumnName="idUser")})
    public Set<UserDAO> getUsers() {
        return users;
    }
    public void setUsers(Set<UserDAO> users) {
        this.users = users;
    }
    public AreaDAO(int idArea, String areaName, AreaDAO parent) {
        super();
        this.idArea = idArea;
        this.areaName = areaName;
        this.parent = parent;
    }
    public AreaDAO() {
        super();
    }
}

UserDAO.java:

@Entity
@Table(name= "user")
public class UserDAO {


    private int idUser;
    private String username;
    private String password;

    private Set<AreaDAO> areas = new HashSet<AreaDAO>();

    @Id
    @Column(name="idUser")
    @GeneratedValue
    public int getIdUser() {
        return idUser;
    }
    public void setIdUser(int idUser) {
        this.idUser = idUser;
    }

    @Column(name="userName")
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }

    @Column(name="userPassword")
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public UserDAO(int idUser, String username, String password) {
        super();
        this.idUser = idUser;
        this.username = username;
        this.password = password;
    }
    public UserDAO() {}

    @ManyToMany
    @JoinTable(name="area_has_user", 
                joinColumns={@JoinColumn(name="user_idUser", referencedColumnName="idUser")}, 
                inverseJoinColumns={@JoinColumn(name="area_idAarea", referencedColumnName="idArea")})
    public Set<AreaDAO> getAreas() {
        return areas;
    }
    public void setAreas(Set<AreaDAO> areas) {
        this.areas = areas;
    }
}

我在哪里做错了?

根据@JB Nizet的评论编辑:

我删除了AreaDAO上的映射并添加了:

@ManyToMany(mappedBy="areas")
    public Set<UserDAO> getUsers() {
        return users;
    }

导致错误的代码是:

@Override
    public Set<AreaDAO> getAreas(UserDAO user) {

        Set<AreaDAO> areas = new HashSet<AreaDAO>();

        user.setUsername("admin_vejle");

        try {           
            Session session = createSessionFactory().openSession();

            Query query = session.createQuery("from UserDAO where username = :username");
            query.setParameter("username", user.getUsername());
            UserDAO result = (UserDAO)query.uniqueResult();
            if(result != null)
            {
                areas = result.getAreas();
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return areas;
    }

更具体地说,在getAreas方法。

在调试模式下检查结果时,Hibernate将输出:

Hibernate: select areas0_.user_idUser as user_idU1_3_0_, areas0_.area_idAarea as area_idA2_1_0_, areadao1_.idArea as idArea1_0_1_, areadao1_.areaName as areaName2_0_1_, areadao1_.parent as parent3_0_1_, areadao2_.idArea as idArea1_0_2_, areadao2_.areaName as areaName2_0_2_, areadao2_.parent as parent3_0_2_ from area_has_user areas0_ inner join area areadao1_ on areas0_.area_idAarea=areadao1_.idArea left outer join area areadao2_ on areadao1_.parent=areadao2_.idArea where areas0_.user_idUser=?
mar. 08, 2015 12:31:55 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1054, SQLState: 42S22
mar. 08, 2015 12:31:55 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Unknown column 'areadao1_.idArea' in 'field list'

此外,如果尝试访问该对象,则会抛出类似的异常。

我现在要让DAO命名为。

1 个答案:

答案 0 :(得分:1)

表区域的列为“idAarea”。名称中有两个: Aa rea

我在映射中只使用了一个A.

这导致错误。