对于我们的应用程序,我们需要使用 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
我也想得到一个删除事件。
我在使用 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) 如何获取java.sql.Connection
当前事务上下文使用的?或者连接实际上是在事务结束时打开的?
要调用 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 操作
我是 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 程序中调用 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() 时都会执行该过程吗?对于这种行为有任何官方文档吗?
我最近开始使用 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) 快速提问:
我有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) 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:
我已经使用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) entitymanager ×10
hibernate ×7
java ×4
jpa ×4
spring ×3
ejb ×1
jboss ×1
micronaut ×1
plsql ×1
postgresql ×1
sql ×1
symfony ×1
symfony-3.2 ×1