【发布时间】:2018-05-27 02:31:38
【问题描述】:
考虑sn-p-
<?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>
使用 DAO 层-
/** The entity manager. */
@PersistenceContext
private EntityManager entityManager;
@Transactional
public void saveEmployee(){
Employee employee = new Employee();
employee.setName("Some Name");
employee.setAddress("Some Address");
employee.setDesignation("Java EE Technologist");
entityManager.persist(employee);
}
在日志中显示-
org.postgresql.util.PSQLException: 错误: 列中的空值 “employee_id”违反了非空约束
Entity class being-
@Entity
@Table(name = "EMPLOYEE")
public class Employee implements Serializable {
/**
*
*/
private static final long serialVersionUID = 3243984159882700015L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "EMPLOYEE_ID")
public long id;
// other columns
}
在 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)
)
请提出建议。
【问题讨论】:
-
您应该在创建表脚本中定义
employee_id SERIAL以便让dbms 处理id 生成。 -
您可能需要将employee_id设置为序列类型或使用序列来生成身份。
标签: spring postgresql hibernate jpa