访问主键时出现异常

时间:2015-02-01 11:26:27

标签: java hibernate

我在程序中有两个表

        @Entity
        @Table(name="managerlog")
        public class Manager {

        private Integer id;

        private Address address;
        @Id
        @Column(name="id")
        @GeneratedValue
        public Integer getId() {
            return id;
        }
        @OneToOne(mappedBy="mng")
        public Address getAddress() {
            return address;
        }

        public void setId(Integer id) {
            this.id = id;
        }
        public void setAddress(Address address) {
            this.address = address;
        }


        }

地址pojo类

    @Entity
    @Table(name="Address")

    public class Address {

        private Integer id;
        private String locality;
        private Manager mng;

        public Address()
        {

        }
        public Address(String locality) {

            this.locality = locality;
        }
        public Address(Integer id,String locality) {
            this.id=id;
            this.locality = locality;
        }

        @Id
        @GeneratedValue
        @Column(name="id")

        public Integer getId() {
            return id;
        }

        @Column(name="locality")
        public String getLocality() {
            return locality;
        }

        @OneToOne
        @JoinColumn(name="MID")

        public Manager getMng() {
            return mng;
        }
        public void setLocality(String locality) {
            this.locality = locality;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public void setMng(Manager mng) {
            this.mng = mng;
        }


    }

db中的地址表是Manager(id,locality,mid) 问题是我想通过像h这样的hibernate查询来访问基于mid的地址id     查询qry = session.createQuery(“address.id From Address address where address.mid =:MID”); 我设置参数..给我错误

线程“main”中的异常java.lang.IllegalArgumentException:遍历的节点不能为null!     在org.hibernate.hql.ast.util.NodeTraverser.traverseDepthFirst(NodeTraverser.java:63)

..
在这里我知道获取Manager类的方法..从该类获取地址..但如果我有MID(外键)..我不能得到address.id(主键).. AnyOne可以解释一下吗?

1 个答案:

答案 0 :(得分:1)

主要问题是Address类没有属性mid,因为mid只是数据库结构的列。如果要获取该经理的地址,则必须以这种方式更改查询:

Query qry=session.createQuery(" address.id From Address address where address.mid.id=:MID"); 

在这种情况下,你可以设置经理识别器,所有都应该工作