我有两个域模型:“ 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”。
这就是为什么?谢谢 !

最佳答案

您的映射是双向关联。您的UserBean作为父级,而LoginBean作为子级。所以你有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。完全删除userid字段将为您做到这一点,我认为您已经尝试过了。

说明:@ManyToOne标记只是说去查看ManyToOne标记的Class以进行关联映射。当转到UserBean时,它发现LoginBean通过LoginBean的“ user”属性与UserBean关联。因此,hibernate将尝试将UserBean的ID映射到标记为FK(在db中)到用户表的LoginBeans字段。

08-03 23:42