jpa

JPA/toplink heterogeneous list of entities

Colleagues, Using JPA I need resolve following issue: at database level exits 3 entities (saying SuperEntity, DetailsAEntity and DetailsBEntity). SuperEntity contains common part of fields for DetailsAEntity and DetailsBEntity. So the question: is it possible to resolve collection of mixed elements DetailsAEntity and DetailsBEntity from...

JPA, Spring, Hibernate problem with loading entities ManyToMany Assotiation

I'm trying to get data from assotiation N:N, but I receive blank field. There are my Hibernate mapping class: @Entity @Table(name = "companies") public class Company extends NamedEntity { //some_code @ManyToMany( targetEntity = com.hqcargo.shippingstock.domain.CompanyProfile.class, cascade = {CascadeType.ALL, CascadeTy...

JPA Adding Logic to Removing an Entity

I am somewhat new to using JPA -- I'll put that out there right off the bat. I'm getting more familiar with it, but there are big holes in my knowledge right now. I am working on an application that uses JPA, and deletes entities using the EntityManager.remove(Object entity) function. However, the application also links into a third-par...

JSF CRUD application and entities

I'm writing an administration part of the application which is responsible for CRUD operations. I decided to generate the jsf pages from the jpa entities but for some reason I keep getting this error: org.apache.jasper.el.JspELException: /busStop/List.jsp(18,12) '#{busStop.pagingInfo.itemCount == 0}' Error reading 'pagingInfo' on type j...

JPA - create-if-not-exists entity?

I have several mapped objects in my JPA / Hibernate application. On the network I receive packets that represent updates to these objects, or may in fact represent new objects entirely. I'd like to write a method like <T> T getOrCreate(Class<T> klass, Object primaryKey) that returns an object of the provided class if one exists in ...

Testing with Spring - Avoiding false positives?

In the spring documentation regarding testing, it states: Avoid false positives when testing ORM code When you test code involving an ORM framework such as JPA or Hibernate, flush the underlying session within test methods which update the state of the session. Failing to flush the ORM framework's underlying sessi...

make a entity class rows read-only in JPA

I m able to get values using JPA when i have only mapped one table Now when i have gone for mapping related tables i get the error Only one may be defined as writable, all others must be specified read-only. Mapping: org.eclipse.persistence.mappings.OneToOneMapping[userId1] I m using [EclipseLink-0] (Eclipse Persistence Services...

Retrieving SQL enum causes java.lang.ClassCastException: java.lang.Character cannot be cast to java.lang.String

I have a query like this Query importQuery = advertiserDao.getEntityManager().createNativeQuery( "select matching_type from group_key where key_id = " + Key.getkeyId()); String match = (String) importQuery.getSingleResult(); The matching_type is a enum('good', 'bad', 'repared') type. This gives an exception as following: java.la...

How to inject custom object ids into JPA entities

I am using JPA 2 for an enterprise application, and my DBA's just hit me with a twist. They want me to use the group's centralized object ID generator for all my tables. This means rather than using table values or a sequence table, I will need to call a web service to get a batch of ~50 ids. Then, as I persist any new object, I would...

jpql don't load Blob

Hi, I have an entity @Entity @NamedQueries({ @NamedQuery(name = "Item.findAll", query = "select i from Item i"), }) public class Item implements Serializable, WithId, WithNameDescription { @Lob byte[] photo; @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true) TextualInfo english = new TextualInfo(); // more e...

JPA: Merge @OneToMany Duplicate entry error

Hi! I'm alwways getting the following error: Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '2-1' for key 'PRIMARY' Error Code: 1062 Call: INSERT INTO ENTITY_ENTITY (relationships_ID, Entity_ID) VALUES (?, ?) bind => [1, 2] Query: DataModifyQuery(sql="INSERT INTO ENTI...

JPA get values from the collections in entities

Hi, I'm new to JPA and now stuck with a problem! I have two tables Person and PersonAddress. In the Person entity, I have @OneToMany(mappedBy="personid") private Set<Personaddress> personaddressCollection; and public Set<Personaddress> getPersonaddressCollection() { return this.personaddressCollection; } public void setPerson...

Calling stored procedure from Java / JPA

Hi, I am writing a simple web application to call a stored procedure and retrieve some data. Its a very simple application, which interacts with client's database. We pass employee id and company id and the stored procedure will return employee details. Web application cannot update/delete data and is using SQL Server. I am deploying ...

What does JPA EntityManager.getSingleResult() return for a COUNT query?

What does EntityManager.getSingleResult() return for a COUNT query? So.. what is the precise runtime type of foo? Object foo = em.createQuery("SELECT COUNT(t) FROM com.company.Thing t WHERE prop = :param") .setParameter("param", value).getSingleResult(); ...

Using OpenSessionInViewInterceptor to avoid LazyInitializationException

First I need to acknowledge the fact that I'm new to EJB, JPA and Spring, so many of the things I believe as true could be wrong. I'm building an EJB application where there's an stateless session bean used to retrieve many JPA (Hibernate) entities. The problem, which I believe is a widespread problem, is that I cannot traverse the rela...

JPA: caching queries

Hi! I'm using JPA to load and persist entities in my JEE-based web application. Hibernate is used as an implementation of JPA, but I don't use Hibernate-specific features and only work with pure JPA. Here is some DAO class, notice getOrders method: class OrderDao { EntityManager em; List getOrders(Long customerId) { Query q =...

Mapping parent child relationship - composite key formed by fields of two objects

There are three object entities: GrandParent, Parent and Child GrandParent contains grandParentId Parent contains object GrandParent, parentId, some attributes, and a list of Child. Child contains childId and some attributes. There are four tables: GrandParent, Parent, Child, ParentsChildrenList GrandParent whose primary key is {gr...

Modify entity with EntityListener on cascade

Hello. I have a database in which an entity (say, User) has a list of entities (say, List). As a requirement, I must have a denormalized count of the entities on the list: @Entity class User { /* ... */ @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) private List<Friendship> friends; public int friendsCou...

Database modeller for JPA with visual diagrams

I am looking for a tool to help in designing and implementing database and entities for a Java project. Probably we are using JPA or direct Hibernate. The features I look for are: Design database with visual modeller Create entities from the visual model Update entity changes to the visual model Import existing database schema as visua...

JPA Glassfish Database Update Issue

Hi, I have an application deployed on Glassfish v3.0.1 which reads events from a table in my database. Once ready it marks them as processed. I am getting a strange error I can't explain when trying to call the method which does the update. @Override @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public void markEventAsPr...