gpt4 book ai didi

java - 如何将参数传递给hibernate AttributeConverter

转载 作者:行者123 更新时间:2023-12-02 09:15:03 25 4
gpt4 key购买 nike

如果我有一个 AttributeConverter 在将输入保留到数据库之前更改输入的长度,那么确保修改后的输入不超过允许的最大长度的正确方法是什么按该列(无需在转换器中对其进行硬编码)?

@Column(length = 1024)
@Convert(converter = MyConverter.class)
private String comment;

public class MyConverter implements AttributeConverter<String, String> {

@Override
public String convertToDatabaseColumn(String attribute) {
return "hello world " + attribute;
}
...
}

最佳答案

使用 hibernate 可以实现你想要的 custom basic type实现DynamicParameterizedType接口(interface)。

下面您可以看到一个声明自定义类型的简单示例,该示例读取@Column注释的length属性。

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Objects;
import java.util.Properties;

import javax.persistence.Column;

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

public class PersistableString implements UserType, DynamicParameterizedType
{
private int sqlType;
private int columnLength;

public PersistableString()
{
this.sqlType = Types.VARCHAR;
}

@Override
public void setParameterValues(Properties parameters)
{
ParameterType reader = (ParameterType) parameters.get(PARAMETER_TYPE);
this.columnLength = getLength(reader);
}

private int getLength(ParameterType reader)
{
int length = -1; // default length
for (Annotation annotation : reader.getAnnotationsMethod()){
if (annotation instanceof Column) {
length = ((Column) annotation).length();
}
}
return length;
}

@Override
public int[] sqlTypes()
{
return new int[] {sqlType};
}

@Override
public Class<?> returnedClass()
{
return String.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);
}

/*
This method will be called when hibernate initializes your entity's
field from the appropriate database table row
*/
@Override
public Object nullSafeGet(ResultSet rs,
String[] names,
SharedSessionContractImplementor session,
Object owner) throws HibernateException, SQLException
{
// you can use this.columnLength here
return rs.getString(names[0]);
}

/*
This method will be called when hibernate persists your entity's field
to the appropriate database table row
*/
@Override
public void nullSafeSet(PreparedStatement st,
Object value,
int index,
SharedSessionContractImplementor session) throws HibernateException, SQLException
{
// you can use this.columnLength here
if (value == null) {
st.setNull(index, sqlType);
}
else {
String val = (String) value;
st.setString(index, val);
}
}

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

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

@Override
public Serializable disassemble(Object value) throws HibernateException
{
return Objects.toString(value);
}

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

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

和用法:

import org.hibernate.annotations.Type;


@Entity
@Table
public class Account
{

@Column(name = "acc_name", length = 50)
@Type(type = "com.example.hibernate.PersistableString")
private String name;

@Column(name = "acc_pass", length = 30)
@Type(type = "com.example.hibernate.PersistableString")
private String pass;

@Column(name = "acc_email", length = 355)
@Type(type = "com.example.hibernate.PersistableString")
private String email;
}

关于java - 如何将参数传递给hibernate AttributeConverter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60278233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com