EntityManagerpersist()不将对象保存到Oracle

时间:2019-06-05 16:41:14

标签: spring hibernate jpa

使用Spring,Hibernate和JPA将员工对象保存到Oracle 12c。运行SpringDataSiebel“ main”时没有收到任何错误,但实际上没有保存到数据库。

<context:component-scan base-package="employee"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="url" value="jdbc:oracle:thin:@...."/>
    <property name="username" value="username"/>
    <property name="password" value="password"/>
</bean>
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="jcgPersistence"/>
    <property name="dataSource" ref="dataSource"/>
    <property name="persistenceXmlLocation" value="META-INF/persistence.xml"/>
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
    <property name="jpaDialect" ref="jpaDialect"/>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">validate</prop>
            <!--prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop-->
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
        </props>
    </property>
</bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaDialect" ref="jpaDialect"/>
</bean>

公共类SpringDataSiebel {

public SpringDataSiebel() {
super();
}

public static void main(String[] args) {
//SpringDataSiebel s = new SpringDataSiebel();

try {
ApplicationContext context = new ClassPathXmlApplicationContext("META-INF\\spring-config-siebel.xml");

//Fetch the DAO from Spring Bean Factory
EmployeeDao employeeDao = (EmployeeDao) context.getBean("EmployeeDaoImpl");
Employee employee = new Employee("Employee 320");
int id = 330;
employee.setEmployeeId(id);

//Save an employee Object using the configured Data source
employeeDao.save(employee);
System.out.println("Employee Saved with EmployeeId " + employee.getEmployeeId());

//find an object using Primary Key
Employee emp = employeeDao.findByPrimaryKey(employee.getEmployeeId());
System.out.println("Found: " + emp);

Employee emp2 = employeeDao.findByPrimaryKey(1000);
System.out.println("Found: " + emp2);

//Close the ApplicationContext
((ConfigurableApplicationContext) context).close();

} catch (BeansException | SQLException e) {
e.printStackTrace();
}

} }

public class EmployeeDaoImpl implements EmployeeDao {
    public EmployeeDaoImpl() {
        super();
    }

    @PersistenceContext
    private EntityManager entityManager;

    //@Override
    @Transactional
    public void save(Employee employee) {

        try {
        //if (!entityManager.contains(employee)) {
            // persist object - add to entity manager
            entityManager.persist(employee);
            System.out.println("Saving the employee object...");

        //}

        } catch (Exception ex)        {
            System.out.println("Something went wrong.");
            ex.printStackTrace();
        }            
    }

}

输出:

保存员工对象... 使用EmployeeId 330保存的员工 找到:null 找到:null 进程退出,退出代码为0。

我希望它找到对象“ EmployeeId 330”

0 个答案:

没有答案