jpa-2.0

How to query an M:N relationship with JPA2?

一世执手 提交于 2019-11-26 20:45:59
问题 I have an an object (BlogPost) that contains an M:N collection of elements (Tags). How to query for an object (BlogPost) where at least one it its Tags matches an element in a set of Tags (defined by the user) with JPA2 (Hibernate). findBlogPostWithAtLeastOneMatchingTag(Collection<Tag> tags){ ???? } My main problem is, that I actually need to compare two collections of tags: - the collection of tags of the BlogPost. - the collection I search for I tried Select p from Post p where p.tags in(

JPA/Hibernate Static Metamodel Attributes not Populated — NullPointerException

一曲冷凌霜 提交于 2019-11-26 20:42:53
I would like to use JPA2 Criteria API with metamodel objects, which seems to be pretty easy: ... Root<JPAAlbum> albm = cq.from(JPAAlbum.class); ... albm.get(JPAAlbum_.theme) ... ; but this Root.get always throws a NullPointerException . JPAAlbum_.theme was automatically generated by Hibernate and looks like public static volatile SingularAttribute<JPAAlbum, JPATheme> theme; but it's obviously never populated. Am I missing a step in the initialization of the framework ? EDIT: here is a snippet of how I use JPA and the metamodel when it's crashing: CriteriaBuilder cb = em.getCriteriaBuilder();

JPA 2.0 many-to-many with extra column

亡梦爱人 提交于 2019-11-26 20:22:28
I'm trying to do a ManyToMany relationship in JPA 2.0 (JBoss 7.1.1) with an extra column (in bold, below) in the relationship, like: Employer EmployerDeliveryAgent DeliveryAgent (id,...) (employer_id, deliveryAgent_id, **ref**) (id,...) I wouldn't like to have duplicate attributes, so I would like to apply the second solution presented in http://giannigar.wordpress.com/2009/09/04/mapping-a-many-to-many-join-table-with-extra-column-using-jpa/ . But I can't get it to work, I get several errors like: Embedded ID class should not contain relationship mappings (in fact the spec says so); In

Cannot make @ManyToOne relationship nullable

拜拜、爱过 提交于 2019-11-26 20:14:28
问题 I have a many-to-one relationship that I want to be nullable: @ManyToOne(optional = true) @JoinColumn(name = "customer_id", nullable = true) private Customer customer; Unfortunately, JPA keeps setting the column in my database as NOT NULL. Can anyone explain this? Is there a way to make it work? Note that I use JBoss 7, JPA 2.0 with Hibernate as persistence provider and a PostgreSQL 9.1 database. EDIT : I found the cause of my problem. Apparently it is due to the way I defined the primary key

JPA CriteriaBuilder - How to use “IN” comparison operator

ぐ巨炮叔叔 提交于 2019-11-26 20:07:43
Can you please help me how to convert the following codes to using "in" operator of criteria builder? I need to filter by using list/array of usernames using "in". I also tried to search using JPA CriteriaBuilder - "in" method but cannot find good result. So I would really appreciate also if you can give me reference URLs for this topic. Thanks. Here is my codes: //usersList is a list of User that I need to put inside IN operator CriteriaBuilder builder = getJpaTemplate().getEntityManagerFactory().getCriteriaBuilder(); CriteriaQuery<ScheduleRequest> criteria = builder.createQuery

What is the purpose of AccessType.FIELD, AccessType.PROPERTY and @Access

会有一股神秘感。 提交于 2019-11-26 18:47:19
I just want to know what is the difference between all these annotations. Why are we using these... means they have no effect especially field level and property level. And what is the purpose of using mixed level annotation like: @Entity @Access(AccessType.FIELD) class Employee { // why their is a field level access private int id; // whats the purpose of transient here @Transient private String phnnumber; // why its a property level access @Access(AccessType.property) public String getPhnnumber() { return "1234556"; } } what exactly this class says? By default the access type is defined by

@OrderColumn annotation in Hibernate 3.5

吃可爱长大的小学妹 提交于 2019-11-26 18:45:22
I'm trying to use the @OrderColumn annotation with Hibernate 3.5 @OneToMany(mappedBy = "parent",fetch=FetchType.EAGER, cascade=CascadeType.ALL) @OrderColumn(name = "pos") private List<Children> childrenCollection; When retrieving data, everything works fine. But I can't make it reorder elements in the List and save the new order to the database. 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

JPA Criteria builder IN clause query

拜拜、爱过 提交于 2019-11-26 18:23:47
问题 How to write criteria builder api query for below given JPQL query? I am using JPA 2.2 . SELECT * FROM Employee e WHERE e.Parent IN ('John','Raj') ORDER BY e.Parent 回答1: This criteria set-up should do the trick: CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Employee> q = cb.createQuery(Employee.class); Root<Employee> root = q.from(Employee.class); q.select(root); List<String> parentList = Arrays.asList(new String[]{"John", "Raj"}); Expression<String> parentExpression

JPA Query selecting only specific columns without using Criteria Query?

北城以北 提交于 2019-11-26 17:29:47
Is it possible to select, say, only properties A and B from an object using a JPA query without using criteria queries? To select all properties I'd just do something like: SELECT i FROM ObjectName i WHERE i.id = 10 But I have an object with many properties on a legacy system, and want to select just a few even though I'm aware selecting several properties is usually quick. Is this possible without using criteria queries? Thank you! 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

How to properly express JPQL “join fetch” with “where” clause as JPA 2 CriteriaQuery?

拜拜、爱过 提交于 2019-11-26 17:22:54
Consider the following JPQL query: SELECT foo FROM Foo foo INNER JOIN FETCH foo.bar bar WHERE bar.baz = :baz I'm trying to translate this into a Critieria query. This is as far as I have gotten: CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Foo> cq = cb.createQuery(Foo.class); Root<Foo> r = cq.from(Foo.class); Fetch<Foo, Bar> fetch = r.fetch(Foo_.bar, JoinType.INNER); Join<Foo, Bar> join = r.join(Foo_.bar, JoinType.INNER); cq.where(cb.equal(join.get(Bar_.baz), value); The obvious problem here is that I am doing the same join twice, because Fetch<Foo, Bar> doesn't seem to have a