如何使用 jpa 的枚举作为持久实体的数据成员?

发布于 2024-08-16 08:09:00 字数 306 浏览 4 评论 0原文

请提供使用 enum 和 jpa 作为持久实体的数据成员的最佳实践和“如何”。 最佳做法是什么? 我想保留枚举中的“C”、“O”。 (代码)。如果这不是正确的方法,请提出建议。

枚举定义是——

public enum Status{
CLOSED ("C")
OPEN ("O")
private final int value;
private Status(final int pValue){
this.value = pValue;
}

public int value(){
 return this.value;
}

Please best practice and 'how to' for using enum with jpa as a data member of persisted entity.
what is the best practice?
I want to persist "C", "O" from enum. (codes). If this is not the correct approach please suggest.

Enum defination is --

public enum Status{
CLOSED ("C")
OPEN ("O")
private final int value;
private Status(final int pValue){
this.value = pValue;
}

public int value(){
 return this.value;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

相对绾红妆 2024-08-23 08:09:00

我们将枚举持久化为字符串。使用 @Enumerated(EnumType.STRING) 来使用字符串表示形式(而不是自动枚举代码)。这使得数据库中的数据更具可读性。

如果您确实需要将枚举映射到特殊代码(旧代码等),则需要自定义映射。首先是一个基类,它将枚举映射到数据库并返回:

import java.io.Serializable;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;


public abstract class CustomEnumType implements UserType
{
    public Object deepCopy (Object value) throws HibernateException
    {
        return value;
    }

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

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

    public boolean equals (Object x, Object y) throws HibernateException
    {
        // Correct for Enums
        return x == y;
    }

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

    public boolean isMutable ()
    {
        return false;
    }

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

    public int[] sqlTypes ()
    {
        return new int[]{ Hibernate.STRING.sqlType() };
    }

}

现在是一个扩展,它使用 DBEnum 将枚举的值映射到数据库并返回:

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.apache.log4j.Logger;

public abstract class DBEnumType extends CustomEnumType
{
    private final static Logger log = Logger.getLogger(DBEnumType.class);
    private static final boolean IS_VALUE_TRACING_ENABLED = log.isTraceEnabled();

    public Object nullSafeGet (ResultSet rs, String[] names, Object owner)
            throws HibernateException, SQLException
    {
        String value = rs.getString (names[0]);
        if (rs.wasNull ())
            return null;

        return toEnum (value);
    }

    public abstract Object toEnum (String value);

    public void nullSafeSet (PreparedStatement st, Object value, int index)
            throws HibernateException, SQLException
    {
        if (value == null)
            st.setNull (index, Hibernate.STRING.sqlType ());
        else
        {
            DBEnum dbEnum = (DBEnum)value;
            value = dbEnum.getDBCode();
            st.setString (index, dbEnum.getDBCode());
        }

        if (IS_VALUE_TRACING_ENABLED)
        {
            log.trace (getClass().getName()+" "+value);
        }
    }
}

接口:

public interface DBEnum
{
    public String getDBCode ();
}

最后,您必须为每个枚举扩展 DBEnumType您想要映射的类型:

public class DBEnumCardType extends DBEnumType
{
    public Class returnedClass ()
    {
        return Status.class;
    }

    public Object toEnum (String value)
    {
        return Status.fromDBCode (value);
    }

}

Status中,您必须实现将数据库代码映射到枚举的静态方法:

    private final static Map<String, Status> dbCode2Enum = new HashMap<String, Status> ();
    static {
        for (Status enm: Status.values ())
        {
            String key = enm.getDBCode ();
            if (dbCode2Enum.containsKey (key))
                throw new ShouldNotHappenException ("Duplicate key "+key+" in "+enm.getClass ());
            dbCode2Enum.put (key, enm);
        }
    }

    private String dbCode;

    private Status (String dbCode)
    {
        this.dbCode = dbCode;
    }

    public String getDBCode ()
    {
        return dbCode;
    }

    public static Status fromDBCode (String dbCode)
    {
        if (dbCode == null)
            return null;

        Status result = dbCode2Enum.get (dbCode);
        if (result == null)
            throw new ShouldNotHappenException ("Can't find key "+dbCode+" in "+Status.class);

        return result;
    }

最后,您必须使用注释@org.hibernate.annotations.Type()< /code> 告诉 Hibernate 使用自定义映射。

结论:不要使用自定义代码。它们只是生成大量你无法分解的愚蠢的样板代码。

We persist enums as strings. Use @Enumerated(EnumType.STRING) to use the string representation (instead of the automatic enum code). That makes your data in the DB much more readable.

If you really need to map the enums to special codes (legacy code and the like), you'll need a custom mapping. First a base class which maps enums to the DB and back:

import java.io.Serializable;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;


public abstract class CustomEnumType implements UserType
{
    public Object deepCopy (Object value) throws HibernateException
    {
        return value;
    }

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

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

    public boolean equals (Object x, Object y) throws HibernateException
    {
        // Correct for Enums
        return x == y;
    }

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

    public boolean isMutable ()
    {
        return false;
    }

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

    public int[] sqlTypes ()
    {
        return new int[]{ Hibernate.STRING.sqlType() };
    }

}

Now an extension which uses DBEnum to map values for your enums to the DB and back:

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.apache.log4j.Logger;

public abstract class DBEnumType extends CustomEnumType
{
    private final static Logger log = Logger.getLogger(DBEnumType.class);
    private static final boolean IS_VALUE_TRACING_ENABLED = log.isTraceEnabled();

    public Object nullSafeGet (ResultSet rs, String[] names, Object owner)
            throws HibernateException, SQLException
    {
        String value = rs.getString (names[0]);
        if (rs.wasNull ())
            return null;

        return toEnum (value);
    }

    public abstract Object toEnum (String value);

    public void nullSafeSet (PreparedStatement st, Object value, int index)
            throws HibernateException, SQLException
    {
        if (value == null)
            st.setNull (index, Hibernate.STRING.sqlType ());
        else
        {
            DBEnum dbEnum = (DBEnum)value;
            value = dbEnum.getDBCode();
            st.setString (index, dbEnum.getDBCode());
        }

        if (IS_VALUE_TRACING_ENABLED)
        {
            log.trace (getClass().getName()+" "+value);
        }
    }
}

The interface:

public interface DBEnum
{
    public String getDBCode ();
}

Lastly, you must extend DBEnumType for each enum type you want to map:

public class DBEnumCardType extends DBEnumType
{
    public Class returnedClass ()
    {
        return Status.class;
    }

    public Object toEnum (String value)
    {
        return Status.fromDBCode (value);
    }

}

In Status, you must implement the static method which maps DB codes to enums:

    private final static Map<String, Status> dbCode2Enum = new HashMap<String, Status> ();
    static {
        for (Status enm: Status.values ())
        {
            String key = enm.getDBCode ();
            if (dbCode2Enum.containsKey (key))
                throw new ShouldNotHappenException ("Duplicate key "+key+" in "+enm.getClass ());
            dbCode2Enum.put (key, enm);
        }
    }

    private String dbCode;

    private Status (String dbCode)
    {
        this.dbCode = dbCode;
    }

    public String getDBCode ()
    {
        return dbCode;
    }

    public static Status fromDBCode (String dbCode)
    {
        if (dbCode == null)
            return null;

        Status result = dbCode2Enum.get (dbCode);
        if (result == null)
            throw new ShouldNotHappenException ("Can't find key "+dbCode+" in "+Status.class);

        return result;
    }

Lastly, you must use the annotation @org.hibernate.annotations.Type() to tell Hibernate to use the custom mapping.

Conclusion: Don't use custom codes. They just generate a lot of stupid boiler plate code which you can't factor out.

成熟的代价 2024-08-23 08:09:00

预期解决方案:
枚举定义:

public enum Status {
    CLOSED(1), NEW(2), RUNNING(3), OPEN(4), ADDED(5), SUCEESS(-1), DONE(0);
        private int code;
        private Status(int code) {
        this.code = code;
    }
        public int getCode() {
        return code;
    }
        public void setCode(int code) {
        this.code = code;
    }
        public static Status valueOf(int i){
        for (Status s : values()){
            if (s.code == i){
                return s;
            }
        }
        throw new IllegalArgumentException("No matching constant for " + i);
    }

}

实体定义:

@Entity
@NamedQuery(name="Process.findAll", query="select p from Process p ")
public class Process {

    @Id
    private long id;
    private String name;

    @Transient
    private transient Status status; //actual enum; not stored in db
        @Column(name="STATUS")  
    private int statusCode; // enum code gets stored in db

    @PrePersist
    void populateDBFields(){
        statusCode = status.getCode();
    }

    @PostLoad
    void populateTransientFields(){
        status = Status.valueOf(statusCode);
    }
    public long getId() {
        return id;
    }
        public void setId(long id) {
        this.id = id;
    }
        public String getName() {
        return name;
    }
        public void setName(String name) {
        this.name = name;
    }
        public Status getStatus() {
        return status;
    }
        public void setStatus(Status status) {
        this.status = status;
    }
}

expected Solution:
enum defination:

public enum Status {
    CLOSED(1), NEW(2), RUNNING(3), OPEN(4), ADDED(5), SUCEESS(-1), DONE(0);
        private int code;
        private Status(int code) {
        this.code = code;
    }
        public int getCode() {
        return code;
    }
        public void setCode(int code) {
        this.code = code;
    }
        public static Status valueOf(int i){
        for (Status s : values()){
            if (s.code == i){
                return s;
            }
        }
        throw new IllegalArgumentException("No matching constant for " + i);
    }

}

Entity Definition:

@Entity
@NamedQuery(name="Process.findAll", query="select p from Process p ")
public class Process {

    @Id
    private long id;
    private String name;

    @Transient
    private transient Status status; //actual enum; not stored in db
        @Column(name="STATUS")  
    private int statusCode; // enum code gets stored in db

    @PrePersist
    void populateDBFields(){
        statusCode = status.getCode();
    }

    @PostLoad
    void populateTransientFields(){
        status = Status.valueOf(statusCode);
    }
    public long getId() {
        return id;
    }
        public void setId(long id) {
        this.id = id;
    }
        public String getName() {
        return name;
    }
        public void setName(String name) {
        this.name = name;
    }
        public Status getStatus() {
        return status;
    }
        public void setStatus(Status status) {
        this.status = status;
    }
}
无敌元气妹 2024-08-23 08:09:00

JPA 默认支持枚举,但问题是它们默认使用您无法控制的值的序数。
为了解决这个问题,你可以在 getter setter 上使用一些逻辑。

@Column(name = "KIRSAL_METROPOL")
private String someEnum;
public YourEnum getSomeEnum() {
    return EnumUtils.getEnum(YourEnum.class, this.someEnum);
}

public void setSomeEnum(YourEnum someEnum) {
    this. someEnum = EnumUtils.getValue(someEnum);
}

EnumUtils 应该进行转换...

Enums are by default supported by JPA but problem is they use the ordinals of the values by default which you cannot control.
To solve this you could use a little logic on your getter setters.

@Column(name = "KIRSAL_METROPOL")
private String someEnum;
public YourEnum getSomeEnum() {
    return EnumUtils.getEnum(YourEnum.class, this.someEnum);
}

public void setSomeEnum(YourEnum someEnum) {
    this. someEnum = EnumUtils.getValue(someEnum);
}

EnumUtils should do the conversion...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文