java持久性与从数据库读取一起工作,静默地无法插入/更新

发布于 2024-12-10 05:58:19 字数 1977 浏览 0 评论 0原文

我使用 JPA 创建了一个简单的 java web (Vaadin) 应用程序项目,它适用于从数据库读取实体。当我尝试使用 persist() 或 merge() 时,数据库中没有任何反应。 查看日志时,有 SELECT 查询,但没有 INSERT / UPDATE。

我使用 Glassfish 3.1 定义了 JDBC 资源,persistence.xml 来使用该资源 (JTA)。 我正在使用由 Eclipse 自动生成的默认配置文件。

还有另外一个别人做的类似的项目,使用同样的JTA,也没有出现这个问题。查看了它的配置,没有发现任何重要的东西。

这两个项目都不使用事务。

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns persistence/persistence_2_0.xsd">
<persistence-unit name="MapperPersistence">
    <jta-data-source>mapper</jta-data-source>
         <class>pl.bdk.mapper.domain.jpaImpl.MappingImpl</class>
...
         <class>pl.bdk.mapper.domain.jpaImpl.UserImpl</class>
             <properties>
                 <property name="eclipselink.logging.level" value="FINE" />
             </properties>
    </persistence-unit>
</persistence>

Java 代码

public class MappingService {
    public static int createMapping(String clientProductId, User user) {
        MappingImpl mapping = new MappingImpl();
        mapping.setUser(user);
        mappingDao.save(mapping);
        return mapping.getMappingId();
    }
}

...
public class MappingDao {
    public void save(MappingImpl mapping) {
        entityManager.persist(mapping);
    }
}

...
@Entity
@Table("t_mapping")
public class MappingImpl() {
    @Id
    @SequenceGenerator(name="T_MAPPING_MAPPINGID_GENERATOR", sequenceName="S_MAPPING_ID", allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="T_MAPPING_MAPPINGID_GENERATOR")
    private Integer id;

    @ManyToOne
    @JoinColumn(name="user_id")
    private User user;

... getters and setters...
}

I have created a simple java web (Vaadin) application project using JPA and it works for reading entities from database. When I try to use either persist() or merge(), nothing happens in database.
When looking at the logs, there are SELECT queries, but no INSERT / UPDATE.

I am using Glassfish 3.1 with JDBC resource defined, persistence.xml to use that resource (JTA).
I am using default configuration files, generated automatically by Eclipse.

There is also another similar project done by someone else, uses the same JTA and this problem does not appear. Looked over its configuration and didn't find anything important.

Both projects do not use transactions.

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns persistence/persistence_2_0.xsd">
<persistence-unit name="MapperPersistence">
    <jta-data-source>mapper</jta-data-source>
         <class>pl.bdk.mapper.domain.jpaImpl.MappingImpl</class>
...
         <class>pl.bdk.mapper.domain.jpaImpl.UserImpl</class>
             <properties>
                 <property name="eclipselink.logging.level" value="FINE" />
             </properties>
    </persistence-unit>
</persistence>

Java code

public class MappingService {
    public static int createMapping(String clientProductId, User user) {
        MappingImpl mapping = new MappingImpl();
        mapping.setUser(user);
        mappingDao.save(mapping);
        return mapping.getMappingId();
    }
}

...
public class MappingDao {
    public void save(MappingImpl mapping) {
        entityManager.persist(mapping);
    }
}

...
@Entity
@Table("t_mapping")
public class MappingImpl() {
    @Id
    @SequenceGenerator(name="T_MAPPING_MAPPINGID_GENERATOR", sequenceName="S_MAPPING_ID", allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="T_MAPPING_MAPPINGID_GENERATOR")
    private Integer id;

    @ManyToOne
    @JoinColumn(name="user_id")
    private User user;

... getters and setters...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半窗疏影 2024-12-17 05:58:19

好吧,事情看起来很简单,但事实并非如此。

这篇文章对我帮助很大: Hibernate 不保存也不抛出异常?

首先 - JPA 需要某种事务机制。通常它是在下面管理的,不需要设置。
这意味着最简单的方法是创建一个 EJB(企业 Java Bean)项目,其中 EntityManager 将注入 @PersistenceContext。如果希望在本地创建 EntityManager(无 EJB),则仍然必须提供事务机制。更多信息在上面的帖子中。

OK, the matter seemed simple, but it isn't.

This post helped me a lot: Hibernate doesn't save and doesn't throw exceptions?

First of all - JPA needs some kind of transaction mechanism. Normally it is managed underneath and needs no setup.
This means the easiest way is to create an EJB (Enterprise Java Bean) project, where EntityManager would be injected with @PersistenceContext. If one prefers to create the EntityManager locally (no EJB), a transaction mechanism still has to be provided. More info in the above post.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文