jpa

Why won't JPA delete owned entities when the owner entity loses the reference to them?

Hi! I've got a JPA entity "Request", that owns a List of Answers (also JPA entities). Here's how it's defined in Request.java: @OneToMany(cascade= CascadeType.ALL, mappedBy="request") private List<Answer> answerList; And in Answer.java: @JoinColumn(name = "request", referencedColumnName="id") @ManyToOne(optional = false) private Req...

Map database column1, column2, columnN to a collection of elements

In legacy database tables we have numbered columns like C1, C2, C3, C100 or M1, M2, M3, M100. This columns represent BLOB data. It is not possible to change anything it this database. By using JPA Embeddable we map all of the columns to single fields. And then during embedding we override names by using 100 override annotations. Recen...

How can you replicate Hibernate's saveOrUpdate in JPA?

In JPA, is there any way you can replicate Hibernate's saveOrUpdate behavior, saveOrUpdate public void saveOrUpdate(Object object) throws HibernateException Either save(Object) or update(Object) the given instance, depending upon resolution of the unsaved-value checks (see the manual for discussion of unsaved-val...

Loading javassist-ed Hibernate entity

I have a JSF converter that I use for a SelectItem list containing several different entity types. In the getAsString() method I create the string as the class name suffixed with ":" and the ID. MySuperClass superClass = (MySuperClass)value; if(superClass != null) { return String.valueOf(superClass.getClass().getName()+":"+superClass...

In Spring with jpa/hibernate, how do I keep a session open to avoid lazy initialization exceptions?

I currently mark collections in entity beans as eager to avoid getting a lazy initialization exception when I try to access the collection properties after loading the bean with the EntityManager. If I instead leave the collection as lazy loading, how do I keep a session open? I thought about trying @Transactional, but even if that work...

Lazily loading a clob in hibernate

There's a lot one can find about this googling a bit but I haven't quite found a workable solution to this problem. Basically what I have is a big CLOB on a particular class that I want to have loaded on demand. The naive way to do this would be: class MyType { // ... @Basic(fetch=FetchType.LAZY) @Lob public String getBlob() ...

JPA createQuery with a partial composite key

The code schematic below works fine but it breaks our coding guidelines due to the unchecked cast. How can this be re-written to use createQuery or otherwise avoid the unchecked cast? Note 1: using hibernate Note 2: @SuppressWarnings("unchecked") is not the answer @Entity @Table(name="Foo") class Foo { @EmbeddedId FooPK pk; ... ...

Will I get in trouble for changing table and column names from upper to camelcase with JPA?

The column and table names that JPA providers create when you're not using @Column tags and the like, are always capitalized. I think the JPA spec even says they should. But is there a specific technical reason, like backends that are case insensitive? In other words, will my habit of always setting camelcased names with the name argume...

Protocol Buffers with JPA

I'm currently building a P2P-system which uses Protocol Buffers for all communication between the peers. There's also a some centralized parts of the system where the peers communicate with a server. The server uses JPA to store the data it has about the peers. So essentially the clients has its data in Protocol Buffers-messages and the...

Configure hibernate (using JPA) to store Y/N for type Boolean instead of 0/1

Can I setup JPA/hibernate to persist Boolean types as 'Y'/'N' in the database (the column is defined as varchar2(1)). It currently stores them as '0'/'1'. The database is Oracle. ...

No Persistence provider for EntityManager named ...

I have my persistence.xml with the same name, using toplink, under META-INF directory. Then I have my code calling it with... EntityManagerFactory emfdb = Persistence.createEntityManagerFactory("agisdb"); Yet, I got the following error message 2009-07-21 09:22:41,018 [main] ERROR - No Persistence provider for EntityManager named agi...

Limit a JPA ManyToMany based on a value of the joined table

Assuming I have the following mapping (code reduced to focus on problem) public class User { @ManyToMany private List<Product> products; } +----+---------+-----------+--------+ | id | user_id | product_id| status | +----+---------+-----------+--------+ | 1 | 1 | 1 | 1 | | 2 | 1 | 2 | 0 | +---...

List as a named parameter in JPA query using TopLink

In the following JPA query, the :fcIds named parameter needs to be a list of integer values: @NamedQuery(name = "SortTypeNWD.findByFcIds", query = "SELECT s FROM SortTypeNWD s WHERE s.sortTypeNWDPK.fcId IN (:fcIds)") Quite logically, this is what is done when the named query is called: Query findByDatesPlFcIds = em.createNamedQuery("...

References to MappedBySuperclass-classes

I have the following problem. I have an abstract class called Vehicle, this class is annotated with MappedBySuperclass. Then I have lots of classes extending that abstract class, such as Car, Boat, Bicycle, Motorcycle etc etc etc. I also have a class called Person. A person owns a set of vehicles. I would like the relation to look like t...

JPA: Reverse cascading delete

@Entity public class Parent { @Id @GeneratedValue(strategy=GenerationType.TABLE) int id; @OneToMany(cascade=CascadeType.REMOVE) List<Item> children = new ArrayList<Child>(); } @Entity public class Child { @Id @GeneratedValue(strategy=GenerationType.TABLE) int id; } As you can see above, I have a OneToMany-R...

How can I trigger actions in my JPA mapped classes whenever they are saved to the database?

I want to have a function in one of my JPA annotated classes that is called every time the EntityManager saves them to the database (aka event listener). Is that possible? Preferably, I'd like to distinguish whether it is just about to be saved or has just been saved. To accomplish what I want, this time I could probably work around wit...

JPA Entity mapping with no foreign keys available

Hi, I dont really get to work with entity beans, but they are staring at me right now. We have a couple of tables that are related to each other, but there are no foreign keys, and we cannot add any. For this question, the database cannot change, it's just not possible, but as a different solution we might create views. Anyways... I...

How to persist a complicated, nested object with JPA

I'm working on a project which parses information with a NLP library from classified ads on internet forums. After the processing I have ForSaleItem instances and all sorts of details like condition, price etc. When I call the toString() method on these objects, it does not return a string stored in the object itself but rather looks up...

Find primary key created by Hibernate JPA

Short version: Is there a simple way to get the value of a primary key after doing a merge() in Hibernate JPA? Details: I'm making a generic library that uses Hibernate. The library uses JPA's merge() to serialize a POJO to the database. What's the best way to discover the primary key's new value? I'm assuming that the primary key ...

JPA query for discrimator column

I have Entity A. Entity B is child table for A and is abstract with DISCRIMINATOR. Entity C and D extends B with discrimatot values 'A' and 'R' respectively How do I select A which has B with status 'A'. Is some thing like this possible.. select a from A as a join fetch a.b as b where b IS C ?? Or Do I need to fetch b and check as fo...