2

我正在开发一个小型 Spring-Hibernate-Mysql 测试项目,由于某种原因,我的事务没有提交给数据库。

在我的应用程序上下文中,我得到了:

<!-- JTA -->

<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />

</bean>

<tx:annotation-driven transaction-manager="txManager" />

<!-- JPA -->

<jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/myPU" />

<!-- In order to enable EntityManager injection -->
<bean
    class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
    <property name="persistenceUnits">
        <map>
            <entry key="myPU" value="persistence/myPU" />
        </map>
    </property>
</bean>

我的persistence.xml:

<persistence-unit name="myPU" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>jdbc/mysqlResource</jta-data-source>
    <properties>
        <property name="hibernate.connection.shutdown" value="true" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"></property>
        <property name="hibernate.show_sql" value="true" />
    </properties>
</persistence-unit>

我在我的数据库中创建了一个名为“persons”的简单表:

CREATE TABLE persons(
id VARCHAR(255) PRIMARY KEY,
version int, 
full_name VARCHAR(255),
person_id VARCHAR(255),
email VARCHAR(255));

创建实体对应的实体和Dao:

@Entity
@Table(name = "persons")
public class Person implements Serializable {

private static final long serialVersionUID = 4349832844316517922L;

/*--- Members ---*/

/**
 * Hibernate genetared UUID
 */
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
private String id;

@Version
private int version;

@Column(name = "full_name")
private String fullName;

@Column(name = "person_id")
private String personId;

@Column(name = "email")
private String eMail;

/*--- Constructor ---*/

public Person() {
}

/*--- Overridden Methods ---*/

@Override
public boolean equals(Object obj) {

    if ((obj == null) || !(obj instanceof Person)) {
        return false;
    }

    // reference comparison
    if (obj == this) {
        return true;
    }

    final Person other = (Person) obj;

    return new EqualsBuilder().append(getPersonId(), other.getPersonId())
            .append(geteMail(), other.geteMail())
            .append(getFullName(), other.getFullName()).isEquals();
}

/**
 * The unique hash code based on the clients' id and citizenship
 * 
 * {@inheritDoc}
 */
@Override
public int hashCode() {

    return new HashCodeBuilder().append(geteMail()).append(this.geteMail())
            .append(this.getFullName()).toHashCode();
}

/*--- Getters & Setters ---*/

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public int getVersion() {
    return version;
}

public void setVersion(int version) {
    this.version = version;
}

public String getFullName() {
    return fullName;
}

public void setFullName(String fullName) {
    this.fullName = fullName;
}

public String getPersonId() {
    return personId;
}

public void setPersonId(String personId) {
    this.personId = personId;
}

public String geteMail() {
    return eMail;
}

public void seteMail(String eMail) {
    this.eMail = eMail;
}

}

道:

@Repository
public class PersonJpaDao extends BasicJpaDao<Person> implements IPersonDao {

public PersonJpaDao() {
    super(Person.class);
}

}

这是BasicJpaDao:

public class BasicJpaDao<T> implements IBasicDao<T> {

/* --- Members --- */

/** The JPA utility to work with the persistence layer. */
@PersistenceContext
protected EntityManager entityManager;

/** The type of the entity to which this DAO offers access. */
protected Class<T> entityClass;

/* --- Constructors --- */

/**
 * Default constructor.
 * 
 * @param entityClass
 *            The type of the entity to which this DAO offers access.
 */
public BasicJpaDao(Class<T> entityClass) {
    super();
    this.entityClass = entityClass;
}

/* --- Public methods --- */

/**
 * {@inheritDoc}
 */
@Override
public void create(T entity) {
    getEntityManager().persist(entity);
}

/**
 * {@inheritDoc}
 */
@Override
public T read(Object primaryKey) {
    return getEntityManager().find(getEntityClass(), primaryKey);
}

/**
 * {@inheritDoc}
 */
@Override
public T update(T entity) {
    return getEntityManager().merge(entity);
}

/**
 * {@inheritDoc}
 */
@Override
public void delete(T entity) {
    getEntityManager().remove(entity);
}

/**
 * {@inheritDoc}
 */
@Override
public void flush() {
    getEntityManager().flush();
}

/* --- Getters/Setters --- */

/**
 * @return The JPA utility to work with the persistence layer.
 */
public EntityManager getEntityManager() {
    return this.entityManager;
}

/**
 * @param entityManager
 *            The JPA utility to work with the persistence layer.
 */
public void setEntityManager(EntityManager entityManager) {
    this.entityManager = entityManager;
}

/**
 * @return The type of the entity to which this DAO offers access.
 */
public Class<T> getEntityClass() {
    return entityClass;
}

/**
 * @param entityClass
 *            The type of the entity to which this DAO offers access.
 */
public void setEntityClass(Class<T> entityClass) {
    this.entityClass = entityClass;
}

Soo .. 基本上它可以工作,但没有任何承诺,我的意思是,如果我运行

@Transactional(propagation = Propagation.REQUIRED)
private void crearePerson() {
    Person p1 = myDao.read("12345");
    p1.setFullName("kiko too");
    myDao.update(p1);
}

我可以看到(在调试中) p1 从数据库中返回,但更新从未发生。我能找到的唯一接近的是:

JPA - 未提交的事务

我尝试添加

<property name="hibernate.connection.shutdown" value="true" />

在这个线程之后到我的 persistence.xml,但它没有帮助。我还向我的连接池(在我的应用程序服务器 gui 中)添加了一个名为 connection.shutdown 的属性,其值为 true,但它也没有帮助。

更新: 由于我使用的是 JTA,我认为我的事务管理器配置错误。当我使用 org.springframework.orm.jpa.JpaTransactionManager 时,我应该使用 org.springframework.transaction.jta.JtaTransactionManager。所以我改变了我的应用程序上下文,现在我有了:

<bean id="txManager"
    class="org.springframework.transaction.jta.JtaTransactionManager" />

<tx:annotation-driven transaction-manager="txManager" />

不幸的是,我仍然遇到同样的问题:(在我的控制台中,我可以看到如下休眠查询()我已经更改了一些原始实体字段,但这并不重要):

信息:休眠:选择 user0_.id 作为 id0_0_,user0_.email 作为 email0_0_,user0_.full_name 作为 full3_0_0_,user0_.password 作为 password0_0_,user0_.update_by_email 作为 update5_0_0_,user0_.user_name 作为 user6_0_0_,user0_.version 作为来自用户 user0_ 的 version0_0_ user0_.id=?

有任何想法吗?

在此先感谢,瑜伽士

4

3 回答 3

3

它可能不起作用的原因是因为您使用@Transactional的是私有方法。@Transactional对非公共方法没有影响,因为代理生成器会忽略它们。来自Spring 文档

方法可见性和@Transactional

使用代理时,您应该仅将 @Transactional 注释应用于具有公共可见性的方法。如果您使用 @Transactional 注释对受保护的、私有的或包可见的方法进行注释,则不会引发错误,但带注释的方法不会显示配置的事务设置。如果您需要注释非公共方法,请考虑使用 AspectJ(见下文)。

于 2012-05-23T00:51:19.203 回答
1

我遇到了类似的问题,对我来说,修复来自以下内容:

在 PersistenceXML 中:

<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.JBossTransactionManagerLookup">

弹簧文件:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="nameOfUnitInPersistenceXml" />
</bean>

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManagerName" value="java:/TransactionManager" />
</bean>

Java 类(命名持久化上下文)

private EntityManager em;

@PersistenceContext(name="nameOfUnitInPersistenceXml")
public void setEntityManager(EntityManager em) {
    this.em = em;
}
于 2015-12-11T17:59:37.963 回答
0

将“@Transactional”添加到您的 create() 方法中。

编辑:最好将 @Transactional 放在服务层而不是 dao 层,仅供参考

于 2012-05-22T18:43:28.020 回答