Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
java
java
public class Book {

    private int id;
    private OwnerInfo ownerinfo;
    //setters and getters omitted for brewitybrevity
}

@Embeddable
public class OwnerInfo {

    private Address address;
    private Name name;
    //setters and getters omitted for brewitybrevity
}

@Embeddable
public class Name {

    private String name;
    //setters and getters omitted for brewitybrevity
}

and the following map:

Code Block
xml
xml
<map>
 <!-- 'oname' is alias for the actual nested bean property -->
 <entry key="oname" value="ownerinfo.name.name"/>
</map>

...

Code Block
java
java
public class Name {

    private String name;
    public Name() {
    } 
    public Name(String name) {
        this.name = name;
    }
    //setters and getters omitted for brewitybrevity
}

the mapping between "oname" and "ownerinfo.name" will work too.

...

Code Block
java
java
public class Book {

    private String title;
    private Date date;
    private OwnerInfo ownerinfo;
    //setters and getters omitted for brewitybrevity
}

@Embeddable
public class OwnerInfo {

    private Address address;
    private Name name;
    //setters and getters omitted for brewitybrevity
}

@Embeddable
public class Name {

    private String name;
    //setters and getters omitted for brewitybrevity
}

@Embeddable
public class Address {

    private String street;
    //setters and getters omitted for brewitybrevity
}


the following code can be used:

...

Code Block
java
java
public static class BookInfo {
        private int id;
        private String title;

        public BookInfo() {
            
        }
        
        public BookInfo(Integer id, String title) {
            this.id = id;
            this.title = title;
        }
        //setters and getters omitted for brewitybrevity
 }

// actual application code:

SearchCondition<Book> sc = searchContext.getCondition(Book.class);
JPACriteriaQueryVisitor<Book, BookInfo> visitor = 
    new JPACriteriaQueryVisitor<Book, BookInfo>(entityManager, Book.class, BookInfo.class);
sc.accept(visitor);

List<SingularAttribute<Book, ?>> selections = new LinkedList<SingularAttribute<Book, ?>>();
// Book_ class is generated by JPA2 compiler
selections.add(Book_.id);
selections.add(Book_.title);

visitor.selectConstruct(selections);

TypedQuery<BookInfo> query = visitor.getQuery();

List<BookInfo> bookInfo = typedQuery.getResultList();
return bookInfo;

...