在JpaRepository.save(实体e)e上,ms sql server数据库中的主键为负值

问题描述 投票:0回答:1

当我执行JpaRepository.save(实体e)时,在hibernate序列的帮助下生成的主键被保存为通常从-43或-42开始的任何随机值。

我是JPA的春季启动项目。

以下是我的属性文件:

hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
hibernate.hbm2ddl.auto=validate
hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
hibernate.show_sql=false
hibernate.format_sql=true

这是我打电话给我的实体。序列名称 - CPU中已存在CPU_Responses_Seq

@Entity
@Table(name="CPU_Responses")
public class CPUResponses extends BaseEnity{

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

    @Id
    @GeneratedValue(generator="CPUResponseSeq",strategy=GenerationType.SEQUENCE)
    @SequenceGenerator(name="CPUResponseSeq",sequenceName="CPU_Responses_Seq")
    @Column(name = "Response_ID", nullable=false,updatable=false)
    private long responseId;

这是我的持久性配置类

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages= {"package path"})
@PropertySource("classpath:application.properties")
public class PersistanceConfiguration {

    @Autowired
    private Environment env;

    public Environment getEnv() {
        return env;
    }

    public void setEnv(Environment env) {
        this.env = env;
    }

    @Bean
    LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException {
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource());
        entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        entityManagerFactoryBean.setPackagesToScan("entity path");

        Properties jpaProperties = new Properties();

        //Configures the used database dialect. This allows Hibernate to create SQL
        //that is optimized for the used database.
        jpaProperties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect"));

        entityManagerFactoryBean.setJpaProperties(jpaProperties);

        return entityManagerFactoryBean;
    }

    @Bean
    public DataSource dataSource() throws NamingException {
        JndiObjectFactoryBean bean = new JndiObjectFactoryBean(); 
        bean.setJndiName("java:comp/env/jdbc/CPUDB"); 
        bean.setProxyInterface(DataSource.class);
        bean.setLookupOnStartup(false);
        bean.afterPropertiesSet();
        return (DataSource) bean.getObject();
    }

     @Bean
        JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
            JpaTransactionManager transactionManager = new JpaTransactionManager();
            transactionManager.setEntityManagerFactory(entityManagerFactory);
            return transactionManager;
        }

}

我不知道出了什么问题。数据将保存在数据库中,但主键为负数。我在DB中的定序器的最小值为零,因此定序器是正确的。

请帮助

sql-server hibernate annotations spring-data-jpa sequence
1个回答
0
投票

我认为这与Hibernate在其序列生成器中引入的更改有关,请尝试添加

hibernate.id.new_generator_mappings = false或spring.jpa.properties.hibernate.id.new_generator_mappings = false

请注意,“新生成器”与以前的版本不兼容,因此从干净的数据库开始以避免出现问题。

© www.soinside.com 2019 - 2024. All rights reserved.