jpa-2.1

What is the right way to use entitymanager

我的未来我决定 提交于 2020-01-03 16:50:28
问题 I have a REST client in Jersey JAX-RS which takes requests and then using Hibernate's JPA implementation retrieves the data from the database and returns a JSON. Using a shared entity manager, the performance is quite good but if there are multiple requests, I get exceptions from Hibernate, mainly NullPointerException. If I use an entity manager per request I don't get any exceptions but the performance is lower. What is the proper way of handling the EntityManager? EDIT: Following Albert's

Hibernate 4 Wildfly 8 logging not working

蓝咒 提交于 2020-01-03 16:12:21
问题 How do I get hibernate 4 to log via logback? I have a war deployed to wildfly 8 final, and I am using slf4j with logback. The logging setup is working 100% in the application with both the console appender and file appender working as intended. Here is what I did to get slf4j + logback working: Excluded the logging subsystem with jboss-deployment-structure.xml in WEB-INF: <?xml version="1.0" encoding="UTF-8"?> <jboss-deployment-structure> <deployment> <exclude-subsystems> <subsystem name=

IN clause with a composite primary key in JPA criteria

坚强是说给别人听的谎言 提交于 2019-12-30 06:27:10
问题 I have a table named group_table in MySQL with only two columns user_group_id and group_id (both of them are of type VARCHAR ). Both of these columns together form a composite primary key. I need to execute a statement using a sub-select IN() to select rows based on a list of values passed to the query. @Override @SuppressWarnings("unchecked") public List<GroupTable> getList(List<GroupTable> list) { CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder(); CriteriaQuery<GroupTable>

JPA create, edit and delete entities from database

感情迁移 提交于 2019-12-25 07:39:58
问题 How should I manage the create, edit and delete operations with entites as simple as possible? For example: User: public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; private String name; ... // Item can't exist without user @OneToMany(cascade=CascadeType.ALL,mappedBy = "user",orphanRemoval=true) private Set<Item> items = new HashSet<Item>(); public Set<Item> getItems() { return items; } public void addItem(Item item) { items.add(item); } public void

Add WHERE IN clause to JPA Specification

青春壹個敷衍的年華 提交于 2019-12-25 01:37:40
问题 I'm trying to implement search functionality limited by IN clause: I want to implement search implementation with filter limitation: @GetMapping("find") public Page<MerchantUserDTO> getAllBySpecification( @And({ @Spec(path = "name", spec = LikeIgnoreCase.class), @Spec(path = "login", spec = LikeIgnoreCase.class), @Spec(path = "email", spec = LikeIgnoreCase.class), }) Specification<Users> specification, @SortDefault(sort = "login", direction = Sort.Direction.DESC) Pageable pageable ) { return

Why does 'EntityManager.contains(..)' return false even if an entity is contained in DB?

戏子无情 提交于 2019-12-24 19:00:11
问题 I used this JPA: check whether an entity object has been persisted or not to know if i persist or merge my entity , It will look like this : if (!getEntityManager().contains(entity)) { System.out.println(" PERSIST "); } else { System.out.println(" MERGE "); } The case is that - even if I edit my entity - it will not recognized as a merge. How is it possible and how to make it work? 回答1: According to the JPA 2.1 specification (PDF page 72), the EntityManager method public boolean contains

How to do JOIN ON query using Criteria API

拥有回忆 提交于 2019-12-23 16:38:00
问题 Since version 2.1 JPA supports join on . I found few examples how to use join on in JPQL but none for Criteria API, and here is my question: Is JOIN ON is implemented in Criteria APi? And if yes, Can anyone provide example? 回答1: Try something like this CriteriaQuery<Person> crit = cb.createQuery(Person.class); Root<Person> candidateRoot = crit.from(Person.class); Join<Person, Address> addrJoin = candidateRoot.join(Person_.address, JoinType.INNER); addrJoin.on({some predicate}); filling "{some

LocalDate between using JPA 2.1 Criteria API

雨燕双飞 提交于 2019-12-23 03:49:04
问题 In my entity I have two fields : private LocalDate startDate = LocalDate.of(1900, 1, 1); private LocalDate endDate = LocalDate.of(3000, 1, 1); Using JPA Criteria API I want to select entities where LocalDate.now() > startDate and LocalDate.now() < endDate . I tried as following : predicates.add(builder.greaterThan(LocalDate.now(), path.<LocalDate> get(Entity_.startDate))); predicates.add(builder.lessThan(builder.currentDate(), path.<LocalDate> get(Entity_.endDate))); But I get this error :

How do I write a JPA query to populate a data transfer object (different than my @Entity object)?

我怕爱的太早我们不能终老 提交于 2019-12-23 01:53:14
问题 We’re using Java 6, JPA 2.1, and Hibernate 4.3.6.Final. I have the below code that finds our Organization objects … final CriteriaBuilder builder = entityManager.getCriteriaBuilder(); final CriteriaQuery<Organization> criteria = builder.createQuery(Organization.class); final Root<Organization> root = criteria.from(Organization.class); final CriteriaQuery query = buildCriteriaQuery(builder, criteria, root, country, state, organizationTypes, parentOrg, zipCode); final TypedQuery<Organization>

How to mix inheritance type in JPA

这一生的挚爱 提交于 2019-12-23 01:11:38
问题 How to mix single table type inheritance with join table type in the same inheritance tree? I'm not using hibernate just JPA. I know there is no official support for mixed inheritance by reading the JPA spec. I can't change the architecture. It did worked with hibernate but now I need to implement this using openjpa. I'm looking for some workaround. 回答1: this is working for me: Superclass: @Entity @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name = "TYPE