如何使用列名作为键将某些表列映射到Map [英] How to map some table columns to a Map with the column names as key

查看:72
本文介绍了如何使用列名作为键将某些表列映射到Map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这张桌子:

CREATE TABLE user_type (
    id TINYINT NOT NULL AUTO_INCREMENT,
    name VARCHAR(20) UNIQUE NOT NULL,
    ...
    --Some more columns

    can_create BOOLEAN NOT NULL,
    can_edit BOOLEAN NOT NULL,
    can_delete BOOLEAN NOT NULL,
    ...
    --Has 23 more columns that define different permissions

    PRIMARY KEY (id)
);

并希望将其映射到这样的内容:

And would like to map it to something like this:

@Entity
@Table(name = "user_type")
public class UserType{
    @Id
    private Byte id;

    private String name;

    // Some more fields

    //Then this map should contain the remaining 26 permission columns
    //with the column names (can_create, can_edit, etc.) as keys.
    @???
    private Map<String, Boolean> permissions;
}

是否可以像这样将某些列映射到 Map ?在所有示例和问题中,我发现它们只是将两个值映射在一起,而不是带有其值的列名称.

Is it possible to map some columns to a Map like that? In all the examples and questions I found they just map two values together, not a column name with its value.

推荐答案

您可以尝试使用以下方法.

You can try to use the following approach.

  1. 创建一个代表您的权限类型的枚举:

public enum PermissionType
{
   CREATE,
   EDIT,
   DELETE

   // other permissions ...
}

  1. 创建 Permissions 类,该类将保留用户的权限状态.
  1. Create Permissions class that will hold the permissions state for a user.

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class Permissions implements Serializable
{
   private Map<PermissionType, Boolean> permissions;
   
   public Permissions()
   {
      permissions = new HashMap<>(PermissionType.values().length);
      for (PermissionType permType : PermissionType.values())
      {
         permissions.put(permType, false);
      }
   }
   
   public void setPermission(PermissionType name, Boolean value)
   {
      permissions.put(name, value);
   }
   
   public Map<PermissionType, Boolean> getPermissions()
   {
      return permissions;
   }
   
   @Override
   public int hashCode()
   {
      return permissions.hashCode();
   }

   @Override
   public boolean equals(Object obj)
   {
      if (this == obj) return true;
      if (obj == null) return false;
      if (getClass() != obj.getClass()) return false;

      Permissions other = (Permissions) obj;
      return Objects.equals(other.permissions, permissions);
   }
}

  1. 创建休眠自定义基本类型通过以下方式获取 Permissions :
  1. Create hibernate custom basic type for the Permissions in the following way:

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Map;
import java.util.Objects;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.UserType;

public class PermissionUserType implements UserType
{
   private static final int[] SQL_TYPES;
   
   static {
      SQL_TYPES = new int[PermissionType.values().length];
      for (int ind = 0; ind < SQL_TYPES.length; ind++)
      {
         SQL_TYPES[ind] = Types.BOOLEAN;
      }
   }
   
   @Override
   public int[] sqlTypes()
   {
      return SQL_TYPES;
   }

   @Override
   public Class<?> returnedClass()
   {
      return Permissions.class;
   }

   @Override
   public boolean equals(Object x, Object y) throws HibernateException
   {
       return Objects.equals(x, y);
   }

   @Override
   public int hashCode(Object x) throws HibernateException
   {
       return Objects.hashCode(x);
   }

   @Override
   public Permissions nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException
   {
      /*
       * The names are column aliases generated by hibernate, like can_dele5_5_, can_crea3_5_, ...
       **/
      Permissions permissions = new Permissions();
      for (int ind = 0; ind < names.length; ind++)
      {
         Boolean val = rs.getBoolean(names[ind]);
         PermissionType name = PermissionType.values()[ind];
         permissions.setPermission(name, val);
      }
      return permissions;
   }

   @Override
   public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException
   {
       if (Objects.isNull(value))
       {
          for (int ind = 0; ind < SQL_TYPES.length; ind++)
          {
             st.setNull(index + ind, SQL_TYPES[ind]);
          }
       }
       else
       {
          Permissions permissions = (Permissions) value;
          for (Map.Entry<PermissionType, Boolean> permEntry : permissions.getPermissions().entrySet())
          {
             Integer ind = permEntry.getKey().ordinal();
             st.setObject(index + ind, permEntry.getValue(), SQL_TYPES[ind]);
          }
       }
   }

   @Override
   public Permissions deepCopy(Object value) throws HibernateException
   {
      if (value == null) return null;
       
      Permissions oldPerms = (Permissions) value;
      Permissions newPerms = new Permissions();
      for (Map.Entry<PermissionType, Boolean> permEntry : oldPerms.getPermissions().entrySet())
      {
         newPerms.setPermission(permEntry.getKey(), permEntry.getValue());
      }
      return newPerms;
   }

   @Override
   public boolean isMutable()
   {
       return false;
   }

   @Override
   public Serializable disassemble(Object value) throws HibernateException
   {
       return deepCopy(value);
   }

   @Override
   public Object assemble(Serializable cached, Object owner) throws HibernateException
   {
       return deepCopy(cached);
   }

   @Override
   public Object replace(Object original, Object target, Object owner) throws HibernateException
   {
       return deepCopy(original);
   }
}

  1. 然后在您的实体映射中使用此自定义基本类型:

import org.hibernate.annotations.Columns;
import org.hibernate.annotations.Type;

@Entity
@Table(name = "user_type")
public class UserTypeEntity
{
   @Id
   @Column(name = "id")
   private Long id;

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

   @Type(type = "com.me.PermissionUserType")
   @Columns(columns = {
      // the order should be matched with the enum PermissionType
      @Column(name = "can_create"),
      @Column(name = "can_edit"),
      @Column(name = "can_delete")
   })
   private Permissions permissions;
   
   // ...
}

这篇关于如何使用列名作为键将某些表列映射到Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆