gpt4 book ai didi

java - 未使用 hibernate 序列ID生成器

转载 作者:太空宇宙 更新时间:2023-11-04 09:37:32 25 4
gpt4 key购买 nike

我想以2019NEO0125格式生成序列ID,其中00125来自休眠序列发生器。这是我的代码来源:

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
import org.hibernate.validator.constraints.NotEmpty;
@Entity
@Table(name="tabe")
public class Neos implements Serializable{

/**
*
*/
private static final long serialVersionUID = 1L;


@Id()
@Column

@GenericGenerator(
name="reference",
strategy = "ReferenceSequenceGenerator",
parameters = {
@Parameter(name=ReferenceSequenceGenerator.DATE_FORMAT_PARAMETER , value="%tY")
}
)
@GeneratedValue(generator = "reference")
private Long reference;
//....


和发电机

import java.io.Serializable;
import java.util.Calendar;
import java.util.Properties;

import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.id.enhanced.SequenceStyleGenerator;
import org.hibernate.type.LongType;
import org.hibernate.type.Type;

import Neos;


public class ReferenceSequenceGenerator extends SequenceStyleGenerator{


public static final String DATE_FORMAT_PARAMETER = "date_format";
public static final String DATE_FORMAT_DEFAULT = "%tY-%tm";

public static final String NUMBER_FORMAT_PARAMETER = "number_format";
public static final String NUMBER_FORMAT_DEFAULT = "%05d";

public static final String ROOT_PARAMETER = "root";
public static final String ROOT_DEFAULT = "NEO";


public static final String SEPARATOR = "separator";
public static final String SEPARATOR_DEFAULT = "";

private String format;

@Override
public Serializable generate(SessionImplementor session,
Object object) throws HibernateException {
return String.format(format, Calendar.getInstance() , ((Neos)object).getClient(), super.generate(session, object));
}

@Override
public void configure(Type type, Properties params,
Dialect d) throws MappingException {
super.configure(new LongType(), params, d);

String dateFormat = params.getProperty(DATE_FORMAT_PARAMETER, DATE_FORMAT_DEFAULT).replace("%" , "%1$");
String numberFormat = params.getProperty(NUMBER_FORMAT_PARAMETER , NUMBER_FORMAT_DEFAULT).replace("%" , "%2$");
String dateNumberSeparator = params.getProperty(SEPARATOR , SEPARATOR_DEFAULT);
String root = params.getProperty(ROOT_PARAMETER , ROOT_DEFAULT);

System.out.println(dateFormat + " -- " + numberFormat + " -- " + dateNumberSeparator );

this.format = dateFormat+root+dateNumberSeparator+numberFormat;
}

}


所有编译,并自从工作正常。但是当我尝试查看数据库中的数据时,我得到了这些ID:

1
2
3
4
5


我认为我的发电机没有被调用。我在生成函数中添加了 System.out.println,但保存实体时未打印。

我尝试将 reference属性的类型更改为字符串。我有一个例外:

"org.hibernate.PropertyAccessException: could not set a field value by reflection setter of com.afb.dsi.scoring.jpa.Credit.reference",
"could not set a field value by reflection setter of com.afb.dsi.scoring.jpa.Credit.reference",
"Can not set java.lang.String field com.afb.dsi.scoring.jpa.Credit.reference to java.lang.Long"


注意:

休眠注释:3.5.6-最终

deltaspike:1.8.1

jpa:2.0

最佳答案

请使用您的ReferenceSequenceGenerator的完整路径

 @GenericGenerator(
name="reference",
strategy = "Neos.ReferenceSequenceGenerator",
parameters = {
@Parameter(name=ReferenceSequenceGenerator.DATE_FORMAT_PARAMETER , value="%tY")
}
)

关于java - 未使用 hibernate 序列ID生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56325393/

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