当我尝试将两个对象插入表中时,而不是主键自动递增它是用第二个对象的值更新第一个插入的对象。
我有一个名为users.java的实体类
package com.company.entities;
// default package
// Generated Jul 16, 2014 2:33:35 AM by Hibernate Tools 3.4.0.CR1
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
/**
* Users generated by hbm2java
*/
@Entity
@Table(name = "USERS", schema = "DOCTORASSIST")
public class Users implements java.io.Serializable {
private static final long serialVersionUID = 1L;
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "users_seq")
@SequenceGenerator(name = "users_seq", sequenceName="users_seq", allocationSize=1)
private long userId;
private String username;
private String password;
private String status;
private Set<UserRoles> userRoleses = new HashSet<UserRoles>(0);
public Users() {
}
public Users(long userId) {
this.userId = userId;
}
public Users(long userId, String username, String password, String status,
Set<UserRoles> userRoleses) {
this.userId = userId;
this.username = username;
this.password = password;
this.status = status;
this.userRoleses = userRoleses;
}
@Id
@Column(name = "USER_ID", unique = true, nullable = false, precision = 10, scale = 0)
public long getUserId() {
return this.userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
@Column(name = "USERNAME", length = 45)
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name = "PASSWORD", length = 100)
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name = "STATUS", length = 45)
public String getStatus() {
return this.status;
}
public void setStatus(String status) {
this.status = status;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "users")
public Set<UserRoles> getUserRoleses() {
return this.userRoleses;
}
public void setUserRoleses(Set<UserRoles> userRoleses) {
this.userRoleses = userRoleses;
}
}
我试图将两个用户插入到数据库表中,其中user_id为主键,并使用@generated值自动递增主键值
这是测试类
@ContextConfiguration
@TransactionConfiguration(defaultRollback=false)
@RunWith(SpringJUnit4ClassRunner.class)
public class OrderPersistenceTests {
@Autowired
UsersRepository usersRepository;
@Test
@Transactional
public void testSaveUser() throws Exception{
Users users1 = new Users();
Users users2 = new Users();
//users1.setUserId();
users1.setUsername("user1");
users1.setPassword("password10");
users1.setStatus("ACTIVE");
usersRepository.saveAndFlush(users1);
users2.setUsername("user2");
users2.setPassword("password20");
users2.setStatus("ACTIVE");
usersRepository.saveAndFlush(users2);
}
}
APP-context.xml中
<?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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- <mvc:annotation-driven /> <mvc:resources mapping="/resources/**" location="/resources/"
/> -->
<tx:annotation-driven />
<bean id="myDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE" />
<property name="username" value="doctorassist" />
<property name="password" value="Thinkpad" />
</bean>
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />
<property name="database" value="ORACLE" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name='packagesToScan' value="com.company.entities" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" />
<bean id="hibernateExceptionTranslator"
class="org.springframework.orm.hibernate4.HibernateExceptionTranslator" />
<jpa:repositories base-package="com.company.repositories" />
<context:component-scan base-package="com.company.*" />
<!-- <context:component-scan base-package="com.mycompany.myapp" /> -->
</beans>
这是控制台日志
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Hibernate: select users0_.USER_ID as USER1_10_0_, users0_.PASSWORD as PASSWORD10_0_, users0_.STATUS as STATUS10_0_, users0_.USERNAME as USERNAME10_0_ from DOCTORASSIST.USERS users0_ where users0_.USER_ID=?
Hibernate: insert into DOCTORASSIST.USERS (PASSWORD, STATUS, USERNAME, USER_ID) values (?, ?, ?, ?)
Hibernate: update DOCTORASSIST.USERS set PASSWORD=?, STATUS=?, USERNAME=? where USER_ID=?
用户表DDL
create table users(
user_id number(10) primary key not null,
username varchar2(45),
password varchar2(100),
status varchar2(45)
);