JPA 2.0 : Exception to use javax.validation.* package in JPA 2.0

As @Korgen mentioned in comments hibernate-validator-5.x.x isn’t compatible with validation-api-1.0.x. This is because of moving to new specification JSR-303 -> JSR-349. There are two ways to solve this issue: 1. Downgrade hibernate validator version (which is implements JSR-303): <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>4.3.1.Final</version> </dependency> 2. If you don’t want to move back from hibernate validator 5 … Read more

@OrderColumn annotation in Hibernate 3.5

The combination of @OneToMany(mappedBy=”…”) and @OrderColumn is not supported by Hibernate. This JIRA issue tracks a request to throw a more obvious error message when this invalid combination is used: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5390 I think that this isn’t supported mainly because it is an odd relational pattern. The annotations above indicate that the “one” side of the … Read more

JPA Query selecting only specific columns without using Criteria Query?

Yes, like in plain sql you could specify what kind of properties you want to select: SELECT i.firstProperty, i.secondProperty FROM ObjectName i WHERE i.id=10 Executing this query will return a list of Object[], where each array contains the selected properties of one object. Another way is to wrap the selected properties in a custom object … Read more

Storing a Map using JPA

JPA 2.0 supports collections of primitives through the @ElementCollection annotation that you can use in conjunction with the support of java.util.Map collections. Something like this should work: @Entity public class Example { @Id long id; // …. @ElementCollection @MapKeyColumn(name=”name”) @Column(name=”value”) @CollectionTable(name=”example_attributes”, joinColumns=@JoinColumn(name=”example_id”)) Map<String, String> attributes = new HashMap<String, String>(); // maps from attribute name to … Read more

How can I validate two or more fields in combination?

For multiple properties validation, you should use class-level constraints. From Bean Validation Sneak Peek part II: custom constraints: Class-level constraints Some of you have expressed concerns about the ability to apply a constraint spanning multiple properties, or to express constraint which depend on several properties. The classical example is address validation. Addresses have intricate rules: … Read more

JPA CascadeType.ALL does not delete orphans

If you are using it with Hibernate, you’ll have to explicitly define the annotation CascadeType.DELETE_ORPHAN, which can be used in conjunction with JPA CascadeType.ALL. If you don’t plan to use Hibernate, you’ll have to explicitly first delete the child elements and then delete the main record to avoid any orphan records. execution sequence fetch main … Read more