How to define unidirectional OneToMany relationship in JPA

My bible for JPA work is the Java Persistence wikibook. It has a section on unidirectional OneToMany which explains how to do this with a @JoinColumn annotation. In your case, i think you would want: @OneToMany @JoinColumn(name=”TXTHEAD_CODE”) private Set<Text> text; I’ve used a Set rather than a List, because the data itself is not ordered. … 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

In JPA 2, using a CriteriaQuery, how to count results

A query of type MyEntity is going to return MyEntity. You want a query for a Long. CriteriaBuilder qb = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = qb.createQuery(Long.class); cq.select(qb.count(cq.from(MyEntity.class))); cq.where(/*your stuff*/); return entityManager.createQuery(cq).getSingleResult(); Obviously you will want to build up your expression with whatever restrictions and groupings etc you skipped in the example.

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)