2 JPA entities on the same table

Let’s say I’ve a table with 200 columns and most of them are never used.

I map SmallEntity to the 10 columns that are used often. I use it in the associations with other entities. It loads fast, consume few memory and makes me happy.

But sometimes I need to display the 200 columns. I’d like to map the BigEntity class on the 200 columns. It is bound to no other entity, it has no association.

Question: Do you have any experience doing that? Are you aware of any trouble that Hibernate would have, as for example in first level cache, dirty checking and entity lifecycle in general?

DB2 JPA entities from table

I’m trying to generate JPA entities from table through the Eclipse Wizard. The database is AS400-DB2. I’ve set the properties in this way: database: ACG_COMTST URL: jdbc:as400://192.168.3.54 username:

Generate JPA2 Entities from Existing Database using Maven

Generate JPA 2 Entities from existing Database I want to do same but using maven build. Please suggest the plugin. As i googled it, i found the metamodel generation of JPA using JPA annotation entitie

Unable to autodiscover JPA2 entities

I have a maven project, a standalone JPA tool. This main project depends on another maven project containing all the entity classes. However, JPA doesn’t seem to find my entities automatically. My Ecl

How to map 2+ entities to same table in .NET Entity Framework?

I’m trying to create 2 entities that operate as different views on the same underlying db table. When I create these in Visual Studio’s entity model and place an association between them, I get a Ass

Eclipse not creating JPA entities from table

I’m using Eclipse Juno, Hibernate 4.0 and MSSQL 2008. I’ve created a database and have just gone through the process JPA Entities from Tables in Eclipse. About half of the tables did not get created

JPA multiple entities one table/view

I have been tasked with fixing a number of bugs on existing application that makes use of JPA (EclipseLink). In this application it common for there to be multiple entities that indirectly share the

Generate JPA 2 Entities from existing Database

How can I generate JPA2 compliant @Entity from existing Databases?. I found this: Question Still its not clear if JBoss will generate compliant JPA2 and also I would like to know if there is a vendor

How to have 2 collections of the same type in JPA? (EclipseLink)

Wondering how to do what is specified in How to have 2 collections of the same type in JPA?, but with EclipseLink rather than Hibernate. See that post below: I’ve got 2 entities in JPA: Entry and Com

Are domain objects the same as JPA entities?

Are domain objects the same as JPA entities? If I have a value object (e.g. a dollar bill), how can I (or even should I) store that in the datastore as a reference object? What are the subtleties of d

Spring JPA mapping with OpenJPA entities with junction table (many-to-many)

I’m having issues mapping JPA entities that are joined by a junction table. The entities were created through STS 3.4.0 using an OpenJPA 2.3.0 implementation (Spring 4.0.2, Spring data jpa 1.5.0). It

Answers

The most straightforward way to do this is to map properties you don’t use often as lazy:

<property name="extendedProperty" lazy="true" />

... or using Annotations ...

@Basic(fetch = FetchType.LAZY)
String getExtendedProperty() { ... }

Hibernate would not load such properties initially; instead they’ll be loaded on demand (when first accessed). You can force Hibernate to load all properties by using fetch all properties clause in your HQL query.

Another possible scenario is to actually map two completely separate entities to the same table but make one of them immutable. Keep in mind that they will be treated as different entities by Hibernate, with first / second level cache being completely separate for both (which is why immutability is important).

You will NOT be able to achieve this functionality via inheritance mapping because Hibernate always returns an actual concrete entity type. Take a look at my answer to Hibernate Inheritance Strategy question for a detailed explanation.