要通过JPA保存List
下面是一个使用@ElementCollection注解保存List
@Entity
public class EntityWithList {
@Id
private Long id;
@ElementCollection
private List strings;
// getters and setters
}
然后,你可以使用EntityManager来保存EntityWithList对象:
List strings = new ArrayList<>();
strings.add("String 1");
strings.add("String 2");
EntityWithList entity = new EntityWithList();
entity.setId(1L);
entity.setStrings(strings);
EntityManager entityManager = ...; // 获取EntityManager对象
entityManager.getTransaction().begin();
entityManager.persist(entity);
entityManager.getTransaction().commit();
这样,List