Spring 为什么JPA Hibernate在持久化时无法为对象生成标识符?
考虑一下这个片段-Spring 为什么JPA Hibernate在持久化时无法为对象生成标识符?,spring,postgresql,hibernate,jpa,Spring,Postgresql,Hibernate,Jpa,考虑一下这个片段- <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<description>nes dao context configuration.</description>
<!-- JPA Configuration -->
<tx:annotation-driven />
<!-- UAT -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="org.postgresql.Driver" />
<property name="jdbcUrl"
value="jdbc:postgresql://localhost:5432/postgres" />
<property name="properties">
<props>
<prop key="c3p0.acquire_increment">5</prop>
<prop key="c3p0.maxStatementsPerConnection">20</prop>
<prop key="c3p0.maxStatements ">100</prop>
<prop key="c3p0.maxPoolSize">500</prop>
<prop key="c3p0.max_statements">0</prop>
<prop key="c3p0.minPoolSize">5</prop>
<prop key="user">postgres</prop>
<prop key="password">system</prop>
</props>
</property>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<!-- <property name="generateDdl" value="true" /> -->
<property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
</bean>
</property>
<property name="persistenceUnitName" value="" />
<property name="persistenceUnitManager">
<bean
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="defaultDataSource" ref="dataSource" />
</bean>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
p:dataSource-ref="dataSource" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
</bean>
</beans>
在日志中,它显示为-
org.postgresql.util.PSQLException:错误:列中的值为null
“员工id”违反了非空约束
在PostgreSQL端创建的表为-
CREATE TABLE employee
(
employee_id integer NOT NULL,
employee_name text NOT NULL,
employee_address text NOT NULL,
designation text NOT NULL,
CONSTRAINT employee_pkey PRIMARY KEY (employee_id)
)
请建议。它在Postgresql端使用自定义的序列-
CREATE SEQUENCE employee_id_seq
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1;
id列注释为-
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "employee_id_seq")
@SequenceGenerator(name = "employee_id_seq", sequenceName = "employee_id_seq", allocationSize = 1)
@Column(name = "EMPLOYEE_ID", unique = true, nullable = false)
public long id;
您应该在create table script
employee_id SERIAL
中定义,以便让dbms处理id生成。您可能需要将employee_id设置为串行类型或使用序列生成标识。Postgresql使用序列生成器,因此会出现使用JPA自动生成主键的问题。这可能是解决此问题的方法。如果执行ALTER表employee ALTER列employee\u id SET DEFAULT nextval('employee\u id\u seq'::regclass)
,您仍然可以使用strategy=GenerationType.IDENTITY。
CREATE SEQUENCE employee_id_seq
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "employee_id_seq")
@SequenceGenerator(name = "employee_id_seq", sequenceName = "employee_id_seq", allocationSize = 1)
@Column(name = "EMPLOYEE_ID", unique = true, nullable = false)
public long id;