hibernate @GeneratedValue始终为空

c8ib6hqw  于 2023-02-19  发布在  其他
关注(0)|答案(1)|浏览(192)

我正在使用@GeneragedValue为实体列生成序列号。但它始终为空。该列不是ID。我正在将实体代码复制粘贴到此处:

import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.*;

@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "ss_request", schema = "AGENCY_SS_APP")
public class SSRequest {

    private static final long serialVersionUID = 1L;
     //This is the Id and it is getting generated fine
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Schema(description = "ss_requestid")
    @Column(name = "ss_request_id", nullable = false, updatable = false, length = 36)
    @Type(type = "uuid-char")
    private UUID ssRequestId;

    //The problem is with this column. I want the value to be generated like REQ00000001. And increment for every record
@Column(name="ss_request_number", columnDefinition = "serial", updatable = false, nullable = false, length = 11)
    @Schema(description ="Request Number")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "request_seq")
    @GenericGenerator(
            name = "request_seq",
            strategy = "com.vermontmutual.agencyssportal.util.ReqSequenceGenerator",
            parameters = {
                    @Parameter(name = ReqSequenceGenerator.INCREMENT_PARAM, value = "1"),
                    @Parameter(name = ReqSequenceGenerator.VALUE_PREFIX_PARAMETER, value = "REQ"),
                    @Parameter(name = ReqSequenceGenerator.NUMBER_FORMAT_PARAMETER, value = "%08d") })
          private String ssRequestNumber;

这是我的请求序列生成器:包com.佛蒙特互惠.代理体育.实用程序;

import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.enhanced.SequenceStyleGenerator;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.LongType;
import org.hibernate.type.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class ReqSequenceGenerator extends SequenceStyleGenerator {

    Logger LOG = LoggerFactory.getLogger(ReqSequenceGenerator.class);

    public static final String VALUE_PREFIX_PARAMETER = "valuePrefix";
    public static final String VALUE_PREFIX_DEFAULT = "";
    private String valuePrefix;

    public static final String NUMBER_FORMAT_PARAMETER = "numberFormat";
    public static final String NUMBER_FORMAT_DEFAULT = "%d";
    private String numberFormat;

    @Override
    public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
        LOG.info("Inside generate of Request Number:");
        return valuePrefix + String.format(numberFormat, super.generate(session, object));
    }

    @Override
    public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
        LOG.info("Inside configure of Request Number:");
        super.configure(LongType.INSTANCE, params, serviceRegistry);
        valuePrefix = ConfigurationHelper.getString(VALUE_PREFIX_PARAMETER, params, VALUE_PREFIX_DEFAULT);
        numberFormat = ConfigurationHelper.getString(NUMBER_FORMAT_PARAMETER, params, NUMBER_FORMAT_DEFAULT);
    }

}

这是我的列liquibase代码。

<column name="ss_request_number" type="VARCHAR(20)">
     <constraints nullable="false" />
 </column>

我做错了什么?我的序列生成代码,ReqSequenceGenerator根本没有被调用。我正在使用H2数据库。这个问题看起来非常相似:I am trying to use H2 in-memory database and custom @GenericGenerator strategy but internally my customeId generator is not getting called。但是他们在那里生成了@Id字段。在那里我生成了一个列。我应该在列上设置任何约束吗?

mftmpeh8

mftmpeh81#

JPA定义的@GeneratedValue注解 * 仅 * 用于生成@Id值。
要生成其他属性值,需要使用org.hibernate.annotations中定义的自定义注解之一。

相关问题