zoukankan      html  css  js  c++  java
  • hibernate联合主键注解配置

    在网上看到好多方法,结果拿来用还是出现了一些问题。现在整理一下

    1、主键类

    import javax.persistence.Column;
    
    
    public class UserRoleUionPK implements java.io.Serializable {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private String userId;
        private String roleId;
        
        
        /*public UserRoleUionPK() {
            super();
        }
    
        public UserRoleUionPK(String userId, String roleId) {
            this.userId = userId;
            this.roleId = roleId;
        }*/
        @Column(name="user_id")
        public String getUserId() {
            return userId;
        }
    
        public void setUserId(String userId) {
            this.userId = userId;
        }
        @Column(name="role_id")
        public String getRoleId() {
            return roleId;
        }
    
        public void setRoleId(String roleId) {
            this.roleId = roleId;
        }
        /*  @Override  
            public boolean equals(Object o) {  
                if(o instanceof UserRoleUionPK){  
                    UserRoleUionPK key = (UserRoleUionPK)o ;  
                    if(this.getUserId().equals(key.getUserId()) && this.getRoleId().equals(key.getRoleId())){  
                        return true ;  
                    }  
                }  
                return false ;  
            }  
              
            @Override  
            public int hashCode() {  
                return this.roleId.hashCode()+this.userId.hashCode();  
            }  */
    }

    网上有的说必须重写equals和hashcode,还有带参构造和无参构造,其实不然,我在项目中将其注释掉没有发现问题。

    我这边主要注重的一点是@Column注解,之前没有加入@Colunm一直报列名无效的问题。

    entity类

    import javax.persistence.EmbeddedId;
    import javax.persistence.Entity;
    import javax.persistence.Table;
    
    @Entity
    @Table(name = "sys_user_role")
    public class SysUserRole implements java.io.Serializable {
    
        private UserRoleUionPK uionPK;
    
        @EmbeddedId
        public UserRoleUionPK getUionPK() {
            return uionPK;
        }
    
        public void setUionPK(UserRoleUionPK uionPK) {
            this.uionPK = uionPK;
        }
    
    }

    2、主键类

    import javax.persistence.Column;
    
    public class UserRoleUionPK implements java.io.Serializable {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private String userId;
        private String roleId;
    
        @Column(name = "user_id")
        public String getUserId() {
            return userId;
        }
    
        public void setUserId(String userId) {
            this.userId = userId;
        }
    
        @Column(name = "role_id")
        public String getRoleId() {
            return roleId;
        }
    
        public void setRoleId(String roleId) {
            this.roleId = roleId;
        }
        
    }

    entity类

    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.IdClass;
    import javax.persistence.Table;
    
    @Entity
    @Table(name = "sys_user_role")
    @IdClass(UserRoleUionPK.class)
    public class SysUserRole implements java.io.Serializable {
    
        
        private String userId;
        private String roleId;
        @Id
        public String getUserId() {
            return userId;
        }
        public void setUserId(String userId) {
            this.userId = userId;
        }
        @Id
        public String getRoleId() {
            return roleId;
        }
        public void setRoleId(String roleId) {
            this.roleId = roleId;
        }
    
    }
  • 相关阅读:
    C#中Dictionary的用法
    安装 Hyperledger Explorer及翻译中文
    Centos7 区块链 HyperLedger Fabric1.0.0安装
    install Hyperledger Fabric Ubuntu 16.04 x64
    三大方案解决了Intellij IDEA 2017/2018.1.5 输入法 不跟随
    ### Cause: java.sql.SQLException: Connection is read-only. Queries leading to data modification are
    could not find implicit value for parameter param: org.apache.spark.AccumulatorParam
    面试感悟----一名3年工作经验的程序员应该具备的技能
    Jenkins Git Maven搭建自动化部署项目环境 邮件通知
    前缀、中缀、后缀表达式
  • 原文地址:https://www.cnblogs.com/volare/p/3944812.html
Copyright © 2011-2022 走看看