org.hibernate.MappingException:实体映射中的重复列

时间:2015-06-13 04:15:07

标签: java hibernate

我有两个Domain-Models:“UserBean”和“LoginBean”。这是一对多关系,用户有很多登录记录。 LoginBean.userid是UserBean.id的外键。 这是数据库的DDL:

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `username` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  `register_date` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) 
CREATE TABLE `login` (
  `id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `login_date` datetime NOT NULL,
  `login_result` char(1) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`),
  CONSTRAINT `login_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) 
    

    @Entity
    @Table(name = "login")
    public class LoginBean {
        @Id
        @Column(name = "id", nullable = false)
        @GenericGenerator(name = "ddd", strategy = "increment")
        @GeneratedValue(generator = "ddd")
        private Integer id; //PK

        // FK --> user.id
        @Column(name = "user_id", nullable = false)
        private Integer userid;

        @Column(name = "login_date", nullable = false)
        private Date logindate;

        @Column(name = "login_result", nullable = false)
        private Boolean loginresult;

        @ManyToOne
        @ElementCollection(targetClass = fordream.hibernate.bean.UserBean.class)
        private UserBean user;

        public Integer getId() {
            return id;
        }

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

        public Integer getUserid() {
            return userid;
        }

        public void setUserid(Integer userid) {
            this.userid = userid;
        }

        public Date getLogindate() {
            return logindate;
        }

        public void setLogindate(Date logindate) {
            this.logindate = logindate;
        }

        public Boolean getLoginresult() {
            return loginresult;
        }

        public void setLoginresult(Boolean loginresult) {
            this.loginresult = loginresult;
        }

        public UserBean getUser() {
            return user;
        }

        public void setUser(UserBean user) {
            this.user = user;
        }
    }



    @Entity
    @Table(name = "users")
    public class UserBean {
        @Id
        @GenericGenerator(name = "abc", strategy = "increment")
        @GeneratedValue(generator = "abc")
        private Integer id; //PK

        @Column(name = "username")
        private String username;

        @Column(name = "password")
        private String password;

        @Column(name = "register_date")
        private Date register_date;

        @OneToMany(mappedBy = "user", cascade = { CascadeType.MERGE })
        private Set loginset;

        public Set getLoginset() {
            return loginset;
        }

        public void setLoginset(Set loginset) {
            this.loginset = loginset;
        }

        public Integer getId() {
            return id;
        }

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

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public Date getRegister_date() {
            return register_date;
        }

        public void setRegister_date(Date register_date) {
            this.register_date = register_date;
        }
    }

当我运行应用程序时,收到一条消息:Repeated column in mapping for entity: LoginBean column: user_id (should be mapped with insert="false" update="false") 但我检查了每个地方,我从未在“LoginBean”中定义过两次“user_id”。 这就是为什么?谢谢!

2 个答案:

答案 0 :(得分:1)

您的映射是双向关联。将UserBean作为Parent,将LoginBean作为Child。所以你有2个映射到user_id表

 @Column(name = "user_id", nullable = false)
    private Integer userid;

@ManyToOne
    @ElementCollection(targetClass = fordream.hibernate.bean.UserBean.class)
    private UserBean user;

userid和user.id尝试映射到user_id。完全删除用户ID字段将为你做,我认为你已经尝试过。

解释:@ManyToOne标记只是说要查看ManyToOne标记的类以获取关联映射。当它转到UserBean时,它发现LoginBean由" user"与UserBean相关联。 LoginBean的属性。因此,hibernate将尝试将UserBean的id映射到标记为FK(在db中)到用户表的LoginBeans字段。

答案 1 :(得分:0)

这个错误只是因为你两次声明id而id为getter和setter。 即:在LoginBean类和UserBean类中。如果您将从第二个类中删除id,则不会出现错误。您可以编写insertable = false和updateable = false。

私有整数id; // PK

请检查你是否错误地在那里创建了getter和setter。