How to Persist a Property of Type List<String> in Jpa

How to persist a property of type List<String> in JPA?

Use some JPA 2 implementation: it adds a @ElementCollection annotation, similar to the Hibernate one, that does exactly what you need. There's one example here.

Edit

As mentioned in the comments below, the correct JPA 2 implementation is

javax.persistence.ElementCollection

@ElementCollection
Map<Key, Value> collection;

See: http://docs.oracle.com/javaee/6/api/javax/persistence/ElementCollection.html

How to persist a property of type List<Object> in JPA?

I figured out that it was pretty easy.

ObjectA Model

@OneToMany
@JoinTable
(
name="OBJECTA_OBJECTB",
joinColumns={ @JoinColumn(name="ID", referencedColumnName="ID") },
inverseJoinColumns={ @JoinColumn(name="OBJECTB_ID", referencedColumnName="ID") }
)
private List<ObjectB> objectList;

JPA CALL

public void insertIntoObjectList(ObjectB object) {
object.setSomeDataInObjectList(list); // just a random list here
em.persist(object);
}

ObjectB Model

@ManyToOne
ObjectA objectA;

JPA mapping for a List or Set<String>

@ElementCollection has been introduced in JPA v 2.0: the mapping you've done is correct. However make sure the maven hibernate plugin you use is at the correct version. Hibernate itself is compliant with JPA 2.0 starting at version 3.5.

How to query a property of type List<String>in JPA

Note that you are using Hibernate specific feature CollectionOfElements. This is not strictly JPA. You can HQL specific elements function to do this:

select f from Family f WHERE :element in elements(f.elements)

Here is a working example:

import org.hibernate.annotations.CollectionOfElements;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.List;

@Entity
public class Family {
@Id
int id;

String name;

@CollectionOfElements(targetElement = java.lang.String.class)
@JoinTable(name = "elements_family",
joinColumns = @JoinColumn(name = "idFamily")
)
@Column(name = "element", nullable = false)
public List<String> elements;

public static void main(String[] args) throws Exception {
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("mysql");
EntityManager em = emf.createEntityManager();
try {

Family f1 = new Family();
f1.id = 1;
f1.name = "Happy";

f1.elements = Arrays.asList("foo", "bar", "yyy", "zzz");

Family f2 = new Family();
f1.id = 2;
f2.name = "Disfunctional";
f2.elements = Arrays.asList("foo", "bar", "yyy", "xxx");

EntityTransaction tx = em.getTransaction();
tx.begin();
em.persist(f1);
em.persist(f2);
tx.commit();
Query query = em.createQuery(
"select f from Family f WHERE :element in elements(f.elements)");
query.setParameter("element", "bar");
List list = query.getResultList();
assert list.size() == 2;

} finally {
em.close();
emf.close();
}
}
}

For the sake of completeness, persistence.xml

<persistence-unit name="mysql" transaction-type="RESOURCE_LOCAL">
<class>jpa.Family</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/test"/>
<property name="hibernate.connection.username" value="test"/>
<property name="hibernate.connection.password" value="test"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.bytecode.use_reflection_optimizer" value="true"/>
<property name="hibernate.archive.autodetection" value="true"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.generate_statistics" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>

Cannot declare List property in the JPA @Entity class. It says 'Basic' attribute type should not be a container

The error seems not have impact on GAE since I can run the app and store data into storage. I guess it's a bug in IntelliJ IDEA and you can simply ignore it.

Sample Image



Related Topics



Leave a reply



Submit