jpa

Java - JPA - @Basic and @Embedded

I am learning JPA from this tutorial. I have some confusions in understanding the following annotations: @Basic @Embedded Fields of an embeddable type default to persistent, as if annotated with @Embedded. If the fields of embeddable types defualt to persistent, then why would we need @Embedded ...

Client Server Communication and Persistence Frameworks

Hi, When using Persistence frameworks like Hibernate, JPA etc. on the server side, what are the general practices of passing on the data between client and server ? Are there any existing design patterns for the same ? Thanks. ...

JPA transient information lost on create

I have an entity with a transient field. When I want to create a new instance of the object I lose my transient information. The following example demonstrates the issue. For the sake of the example let's say that barness is a transient field. FooEntity fooEntity = new FooEntity(); fooEntity.setFoobosity(5); fooEntity.setBarness(2); ...

archiving strategies and limitations of data in a table

Environment: Jboss, Mysql, JPA, Hibernate Our web application will be catering to a large amount of users (~ 1,000,000) and there are a lots of child table where user specific data are stored (e.g. personal, health, forum contributions ...). What would be the best practice to archive user & user specific information. [a] Would it be...

In a bidirectional JPA OneToMany/ManyToOne association, what is meant by "the inverse side of the association"?

In these examples on TopLink JPA Annotation Reference: Example 1-59 @OneToMany - Customer Class With Generics @Entity public class Customer implements Serializable { ... @OneToMany(cascade=ALL, mappedBy="customer") public Set<Order> getOrders() { return orders; } ... } Example 1-60 @ManyToOne - Order Cla...

Hibernate/JPA DB Schema Generation Best Practices

I just wanted to hear the opinion of Hibernate experts about DB schema generation best practices for Hibernate/JPA based projects. Especially: What strategy to use when the project has just started? Is it recommended to let Hibernate automatically generate the schema in this phase or is it better to create the database tables manually ...

Interfaces with hibernate annotations

Hello i am wondering how i would be able to annotate an interface @Entity @Table(name = "FOLDER_TABLE") public class Folder implements Serializable, Hierarchy { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "folder_id", updatable = false, nullable = false) private int fId; @Column(name = "folder_name") private St...

Java - JPA - When we need more than one EntityManager?

I am learning JPA and have one question: In which situations we need more than one EntityManager in our application? The two situations that I am aware of are as follows: When our application is a multi-threaded application and more than one thread needs JPA transaction because EntityManager is not thread-safe and we need one EntityM...

Java - JPA - Why we need more than one <persistence-unit> tag in persistence.xml file?

I am learning JPA. I read about persistence.xml file. It can contain more that one <persistence-unit> tag under <persistence> tag. Upto my undestanding, <persistence-unit> defines: db connection settings classes(entity classes), jar files, and mapping files provider information Then why would we need to group the entities of our app...

How do I get spring to inject my EntityManager?

I'm following the guide here, but when the DAO executes, the EntityManager is null. I've tried a number of fixes I found in the comments on the guide, on various forums, and here (including this), to no avail. No matter what I seem to do the EntityManager remains null. Here are the relevant files, with packages etc changed to protect ...

Hibernate reverse engineering procedure generated @Temporal(TemporalType.TIMESTAMP) for DateTime

Fields in a DB were of type DATETIME (mysql). Why was it generated as a timestamp and will it be regenerated during object persistence? That is not a desired scenario in my case, seeing that as not a timestamp field means that it should only be set via setter, no generation needed, I dont really understand why it did not generate it...

JPA - Removing entities

I have a Story entity with the following associations: Story <1-*> Chapter Story <1-*> Comment Story <*-1> User What is the correct way of removing this entity and handling the all the entities that is referring to? Is there some shorthand way of specifying that associated entities must be handled automatically or is the @PreRemove a...

Hibernate cascading

All what Hibernate reverse engineering generates is something like this @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "column_id") public Itinerary getColumnId() { return this.columnId; } I want this scenario: when session flushes, first all constructed childs were saved, then the parent object, accord...

How can i resolve the N+1 Selects problem ?

Hello everyone, I have trouble understanding how to avoid the n+1 select in jpa or hibernate. From what i read, there's the 'left join fetch', but i'm not sure if it still works with more than one list (oneToMany).. Could someone explain it to me, or give me a link with a clear complete explanation please ? I'm sorry if this is a noo...

Annotations present at compile time but absent at runtime

It is possible to use Java class files which includes annotations that are not present at runtime? Example: I want to write a class with the JPA @Embeddable annotation, which would be present at compile time (maven scope: "provided"). But the annoatation definition could be absent at runtime, if the class is used outside a JPA applicati...

Hibernate one-to-one: getId() without fetching entire object

I want to fetch the id of a one-to-one relationship without loading the entire object. I thought I could do this using lazy loading as follows: class Foo { @OneToOne(fetch = FetchType.LAZY, optional = false) private Bar bar; } Foo f = session.get(Foo.class, fooId); // Hibernate fetches Foo f.getBar(); // Hibernate fetch...

Example using countDistinct in a JPA Criteria API query

I'm having trouble figuring out how to represent the following JPQL query: SELECT count(e) FROM Foo e using Criteria API. What I'm trying is: CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Foo> c = cb.createQuery(Foo.class); Root<Foo> f = c.from(Foo.class); c.select(cb.count(f)); but this is not working. I also tried: ...

Question on jpa joined table inheritance

Hi, The 'DiscriminatorColumn' annotation isn't creating any column in my parent entity. Where am I going wrong ? Here's my code @Entity @Inheritance(strategy=InheritanceType.JOINED) @DiscriminatorColumn(name="TYPE", discriminatorType=DiscriminatorType.STRING,length=20) public class WorkUnit extends BaseEntityClass implements Serializab...

Java - JPA - Generators - @SequenceGenerator

I am learning JPA and have confusion in the @SequenceGenerator annotation. Upto my understanding, it automatically assigns a value to numeric identity fields/properties of an entity. Q1. Does this sequence generator make use of the database's increasing numeric value generating capability or generates the number on his own? Q2. If JPA...

Is it a good idea to "migrate business logic code into our domain model"?

I am reading Hibernate in Action and the author suggests to move business logic into our domain models (p. 306). For instance, in the example presented by the book, we have three entities named Item, Bid, and User and the author suggests to add a placeBid(User bidder, BigDecimal amount) method to the Item class. Considering that usually...