jpa

JPA Hibernate: How to prevent join on retrieving?

Suppose I have the following objects (one table per object) with this relations: A -> B -> C -> D If I findById an instance of A, all B, C and D are returned which is not I want. Is this possible to force Hibernate only to return A (or only it's primitive properties)? I know that I can write "SELECT a.x, a.y, a.z, ... FROM A" and then ...

JPA parent child delete problem

I have two entities: @Entity public class Game implements Serializable{ @ManyToOne @JoinColumn(name = "target_id") protected GameObject target; } @Entity public class GameObject implements Serializable { @OneToMany(mappedBy = "target", cascade = CascadeType.ALL) protected Collection<Game> games = new HashSet<Game>(); } In one t...

JPA cascade update problem. Am i doing this the wrong way?

Hi everyone, i'm using JPA on a SWING application in JAVA that connects to an Apache DERBY embedded database. I use Netbeans as my IDE and use many of the "suposedly" helpfull templates. My problem it's simple, but it's dificult for me to explain so i will paste the relevant code here and try to explain at the bottom. @Entity public c...

JPA Eclipselink: How to find which relationship is causing error

I get the following error when attempting to persist an object: java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST Is there any simple way to tell which relationship has the problem object? It is possible that the persisted object has many rela...

JPA find the Last entry

Hi, I would like to know what's the best way to get the last entry of a table with JPA. In Sql, what I'm looking for would be like: select id from table order by id desc limit 1 I was thinking about model.count() but that sounds more like a hack than a good solution ;) ...

Spring + Hibernate + JPA

As of now I have a working Spring application with persistence. However now I want to use Hibernate with JPA to do all of my database activities. I want to do this using an entitymanager. I've been reading many documents and tutorials on this matter, I've been getting confused on whether I need a persistence.xml file or not. Also I've ...

Another Spring + Hibernate + JPA question

I'm still struggling with changing my Spring Application to use Hibernate with JPA to do database activities. Well apparently from a previous post I need an persistence.xml file. However do I need to make changes to my current DAO class? public class JdbcProductDao extends Dao implements ProductDao { /** Logger for this class and s...

Why EclipseLink is adding discriminator column for joined inheritance strategy?

I am using JOINED inheritance strategy with EclipseLink JPA implementation. I have noticed that EclipseLink is adding discriminator column, named by default DTYPE, to the database schema. I understand, that discriminator is needed for one table inheritance strategy, but why for JOINED strategy? EclipseLink needs this column because I've...

JPQL to get nearest records by latitude and longitude

I'm have entities with latitude and longitude and I would like to select those nearest to a given point. I found this example that looks like it would work SELECT *, SQRT(POW((69.1 * (locations.latitude - 27.950898)) , 2 ) + POW((53 * (locations.longitude - -82.461517)), 2)) AS distance FROM locations ORDER BY distance ASC LIMI...

JPA and PostgreSQL

I'm on a project that uses the EclipseLink implementation of JPA to talk to a PostgreSQL database. I have a task for which PostgreSQL NOTIFY/LISTEN seems like a perfect fit. Unfortunately, I'm a JPA newb, and am struggling to figure out how to make it work. So I guess I really have two questions; answering either one will make me happ...

Spring + Hibernate JPA Question

I'm trying to use Hibernate with JPA/EntityManager to do database activities Right now I'm getting this error and I have no idea what it means. Before I had this code and it works fine. public class JdbcProductDao extends Dao implements ProductDao { /** Logger for this class and subclasses */ protected final Log logger = LogFactory.g...

how do you enforce conditional not null check across sql columns

create table A (id, col_A, col_B, col_C) id = unique id for every row being persisted either col_A or col_B will have a valid value, but both columns will not have a value for each persisted row at the same time. e.g. insert into A (id, col_A, col_C) values (1, "a", "c") insert into A (id, col_B, col_C) values (1, "b", "c") insert i...

JPA query for DTYPE

I have a single table which is generated from two jpa entities (inheritance). So we have a DTYPE column generated in the database table which has entity name has values. How do I write a jpa query to fetch data for any given entity. i.e DTYPE = 'A'? I just tried using DTYPE in jpa query but as expected I got an error "Could not resolve...

Strange JPA behaviour, initialized field is null

I'm observing a very strange behaviour with an entity class and loading an object of this class whith JPA (hibernate entitymanager 3.3.1.ga). The Class has a (embedded) field, that is initialized in the declaration. The setter of the field implements a null check (i.e. would throw an exception when a null value is set). ... @Entity publ...

Connection with MySql is being aborted automaticly. How to configure Connector/J properly?

I read this advice from error message: You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem. I'm using Spring and JPA. W...

Dependency Injection problem with EntityManagerFactory + jpadaosupport

Right now I'm having a problem injecting a entityFactoryManager into my jpadaosupport extended class. My configuration is below: <bean id="productDao" class="springapp.repository.JdbcProductDao"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> The above configuration for this bean works fine however w...

Design question

Hi everbody, We are building software in java, and are new to it. I confused about JPA. Normally in MVC pattern, SQL queries are hidden in model. And controller can't access the db directly. When I use JPA, should model retrieve JPA object to controller? If yes, then controller has access to db, and this is against to pattern? ...

JPA Criteria API missing

Hello, I'm building application that uses JPA, and I want to use Criteria API as described http://openjpa.apache.org/builds/latest/docs/manual/jpa_overview_criteria.html. More precisely this part: EntityManager em = ... ; CriteriaBuilder queryBuilder = em.getCriteriaBuilder(); CriteriaQuery qdef = queryBuilder.createCriteriaQuery(); ...

Unable to delete entity with complex parent-child relationships

I have the following relationships in my entity table Table A @OneToMany(mappedBy = "A", cascade = CascadeType.ALL, fetch = FetchType.LAZY) @Cascade(value = DELETE_ORPHAN) @OrderBy private List<B> orders = new ArrayList<B>(0); @OneToMany(mappedBy = "A", cascade = CascadeType.ALL, fetch = FetchType.LAZY) @Cascade(value = DELETE_ORPHAN)...

How to get rid of LazyInitializationException with Wicket JPA/Hibernate integration (with Spring)

Hi, i'm developing an application using Wicket as the view layer and JPA(Hibernate) as ORM. building the UI has been fun (even with ajax) using Wicket. My problem comes from integrating the persistent objects on edit pages (readonly pages are no problem using a LoadadableDetachableModel). I'm using the OSIV filter from spring to provide...