标签: entitymanager

用于命名查询删除操作的休眠实体侦听器

对于我们的应用程序,我们需要使用 hibernate 实现触发器。我们可以找到的一个最佳解决方案是使用 Annotation 的 Entity listerner,因为我们需要监听特定的实体更改。

除了带有命名查询的删除之外,一切都运行良好,它不提供任何事件。

代码实现 **实体**——这里我们添加了监听器

@Entity
@EventListeners(EmployeeEventListener.class)
public class Employee {

  @Id
  private String uid;
  @Basic
  private Calendar lastUpdated;
Run Code Online (Sandbox Code Playgroud)

实体监听器-

侦听器在执行预期操作时占用修改后的实体

public class EmployeeEventListener {
  @PrePersist
  public void prePersist(Object object) {
    Employee employee = (Employee)object;
    employee.setUID(UIDGenerator.newUUI());
    employee.setLastUpdated(Calendar.getInstance());
  }
  @PostUpdate
  public void postUpdate(Object object) {
    Employee employee = (Employee)object;
    employee.setLastUpdated(Calendar.getInstance());
  }
Run Code Online (Sandbox Code Playgroud)

@PrePersist并且@PostUpdate运作良好,当我用节省或者saveOrUpdate在实体管理器。但是在执行删除命名查询时,我没有收到@PreRemove@PostRemove

我也想得到一个删除事件。

hibernate entitymanager entitylisteners

4
推荐指数
1
解决办法
2105
查看次数

实体管理器 Find() 方法的正确使用

我在使用 EntityManager Here my User 实体从数据库中获取数据时遇到问题

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonProperty
private Integer id;
@Column(name = "username", length = 20, nullable = false)
@JsonProperty
private String username;
@Column(name = "password", nullable = false, unique = true)
@JsonProperty
private String password;
@Column(name = "enabled", nullable = false)
@JsonProperty
private boolean enabled;
@Column(name = "email", nullable = false, unique = true)
@JsonProperty
private String email;
@OneToMany(mappedBy = "user", cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
private Set<UserRole> userRoles;
//getters and setters
Run Code Online (Sandbox Code Playgroud)

我尝试使用用户名搜索用户:

public …
Run Code Online (Sandbox Code Playgroud)

hibernate entitymanager

4
推荐指数
1
解决办法
6343
查看次数

Spring Data JPA,如何获取当前事务上下文使用的Connection

如何获取java.sql.Connection当前事务上下文使用的?或者连接实际上是在事务结束时打开的?

spring hibernate jpa entitymanager spring-data-jpa

4
推荐指数
1
解决办法
6575
查看次数

使用JPA执行数据库功能

要调用 Oracle 数据库函数,我只需编写

final Query query = entityManager.createNativeQuery(
    "select myfunction(?) from dual"
);
query.setParameter(1, "value");
Object rv = query.getSingleResult();
Run Code Online (Sandbox Code Playgroud)

只要被调用的函数不执行任何 dml 操作,此操作就有效。否则我必须执行类似的事情

    {? = call myfunction(?)}
Run Code Online (Sandbox Code Playgroud)

不幸的是,我无法注册任何 OUT 参数,因此我无法使该语句起作用。在不使用普通 JDBC 的情况下如何实现这一点?


编辑:

这个问题具有误导性。我想获取 Oracle 数据库中函数(不是存储过程)的结果。没有 OUT 参数。该函数可能如下所示:

CREATE OR REPLACE FUNCTION myfunction(value IN VARCHAR2)
RETURN VARCHAR2
IS
BEGIN
    UPDATE foo SET bar = 'foobar'
    WHERE id = 1;

    RETURN 'test';
END myfunction;
Run Code Online (Sandbox Code Playgroud)

Calling an oracle function from JPA 的答案并不能解决我的问题,因为我的函数内部存在 dml 更改。我收到错误:

无法在查询内执行 DML 操作

java hibernate jpa entitymanager

4
推荐指数
1
解决办法
2万
查看次数

如何将实体管理器与 Micronaut 一起使用?

我是 Micronaut 框架的新手,我正在尝试使用实体管理器来创建我的存储库。我像这样创建了我的存储库

public interface EmployeeRepository {
    Employee save(@NotNull Employee employee);
    Employee update(@NotNull Employee employee);
    List<Employee> findAll();
    Optional<Employee> findById(@NotNull Long id);
}
Run Code Online (Sandbox Code Playgroud)

我使用这个类来实现接口并注入实体管理器

@Singleton
public class EmployeeRepositoryImpl implements EmployeeRepository{
    @PersistenceContext
    private EntityManager entityManager;

    public EmployeeRepositoryImpl(@CurrentSession EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Override
    @Transactional
    public Employee save(@NotNull Employee employee) {
        entityManager.persist(employee);
        return employee;
    }

    @Override
    @Transactional
    public Employee update(@NotNull Employee employee) {
        return entityManager.merge(employee);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Employee> findAll() {
        String qlString = "SELECT * FROM Employee"; …
Run Code Online (Sandbox Code Playgroud)

java entitymanager micronaut

4
推荐指数
1
解决办法
6628
查看次数

从存储过程获取输出参数而不调用execute()

我想通过实体管理器从 Java 程序中调用 PL/SQL 存储过程:

StoredProcedureQuery storedProcedureQuery = entityManager.createStoredProcedureQuery("someProcedure");
Run Code Online (Sandbox Code Playgroud)

现在我的理解是,我必须在此storedProcedureQuery 上调用execute() 才能执行该过程并能够检索OUT 参数。但是,我可以通过简单地调用来检索这些值,而无需调用execute()方法

someValue1 = (Integer)storedProcedureQuery.getOutputParameterValue(1);
someValue2 = (Integer)storedProcedureQuery.getOutputParameterValue(2);
Run Code Online (Sandbox Code Playgroud)

这怎么可能 ?这段代码片段中的存储过程到底什么时候执行?每次调用 getOutputParameterValue() 时都会执行该过程吗?对于这种行为有任何官方文档吗?

java sql plsql stored-procedures entitymanager

4
推荐指数
1
解决办法
3702
查看次数

“org.hibernate.PersistentObjectException:分离的实体传递到持久”错误,我无法解决

我最近开始使用 JPA 和 Hibernate 进行一个学校项目,它基本上将各大洲、国家和城市存储在数据库中。

当尝试在实体上使用 persist 方法时,我收到标题中提到的错误。

无论我访问过哪些线程都解决了很多更复杂的问题,并且对我的情况没有真正帮助。我希望通过在这里发帖找到一些指导。

这是数据库创建脚本。这是一个 PostgreSQL 数据库。

create table continents(
    id integer primary key,
    name varchar
);
create table countries(
    id integer primary key,
    name varchar,
    code varchar,
    continent_ID integer,
    constraint fk_continents
                      foreign key (continent_ID)
                      references continents(id)
);
create table cities(
    id integer primary key,
    country_ID integer,
    name varchar,
    hasCapital boolean,
    latitude float,
    longitude float,
    constraint fk_countries
    foreign key (country_ID)
    references countries(id)
);
Run Code Online (Sandbox Code Playgroud)

基本上,国家/地区表使用大陆表的 ID 作为外键,而城市表再次使用国家/地区表的 ID 作为外键。

我得到的错误是由于我试图保留一个新的大陆实体而引起的。这是执行的代码:

public static void main(String[] args) { …
Run Code Online (Sandbox Code Playgroud)

java postgresql hibernate jpa entitymanager

4
推荐指数
1
解决办法
1万
查看次数

在Spring中静态访问实体管理器和不寻常的架构

快速提问:

我有webapplication(wicket + spring + jpa)并且正在考虑相当不寻常的架构设计.请检查出来并发表您的意见.

考虑类包装器:

@Service
public class Wrapper {
    protected static EntityManager entityManager;

    @PersistenceContext
    private void injectEntityManager(EntityManager entityManager) {
        Wrapper.entityManager = entityManager;
    }
Run Code Online (Sandbox Code Playgroud)

如你所见,我现在已经静态注入了EntityManager.

现在考虑简单的实体DogEntity

@Entity
public class DogEntity {
   String name;
}
Run Code Online (Sandbox Code Playgroud)

对于那个实体,我们创建了包装狗

public class Dog extends Wrapper {
  private DogEntity entity;

  private Dog(DogEntity entity) {
     this.entity = entity;
  }

  public static Dog create(String name) {
    entity = new DogEntity();
    entity.name = name;
    entityManager.persist(entity); // for a moment forget that this code is not in transaction …
Run Code Online (Sandbox Code Playgroud)

spring hibernate jpa static-members entitymanager

3
推荐指数
1
解决办法
5009
查看次数

java.lang.NullPointerException upon EntityManager injection in Spring repository

I am new to Spring MVC. I've been searching for few days for a solution to my problem, without any success.

Here is my stack:

  • jBoss 4.2.3GA server
    • I know it's a very old version, but that is what I am limited to right now. jBoss 7.1 will be approved for use in my organization within the next few months, but I would like to make my R&D application work in the 4.2.3GA server. That means I have added all …

jboss spring hibernate ejb entitymanager

3
推荐指数
2
解决办法
9303
查看次数

Symfony 3 - 具有多个数据库连接的EntityManager依赖注入

我已经使用guard设置了一个Custom Authenticator并自动连接了该服务.这经过测试,只需配置MySQL即可正常工作.

我现在已经指定了第二个数据库连接(oracle),但Symfony现在不允许在我的服务配置中进行自动装配,因为它不知道在将EntityManager注入自定义Authenticator类时要使用哪个数据库连接.

知道如何配置依赖注入以使用特定的数据库连接,以便我可以继续使用AutoWire.

Unable to autowire argument of type "Doctrine\ORM\EntityManager" for the service "user.security.login_form_authenticator". Multiple services exist for this class (doctrine.orm.prism_entity_manager, doctrine.orm.baan_entity_manager).
Run Code Online (Sandbox Code Playgroud)

这是我在config.yml中的Doctrine配置

doctrine:
    dbal:
        connections:
            prism:
                driver:   pdo_mysql
                host:     "%database_host%"
                port:     "%database_port%"
                dbname:   "%database_name%"
                user:     "%database_user%"
                password: "%database_password%"
                charset:  UTF8
                # if using pdo_sqlite as your database driver:
                #   1. add the path in parameters.yml
                #     e.g. database_path: "%kernel.root_dir%/../var/data/data.sqlite"
                #   2. Uncomment database_path in parameters.yml.dist
                #   3. Uncomment next line:
                #path:     "%database_path%"
            baan:
                driver:   oci8
                host:     "%baan_host%"
                port: …
Run Code Online (Sandbox Code Playgroud)

dependency-injection entitymanager symfony symfony-3.2

3
推荐指数
2
解决办法
2174
查看次数