简体   繁体   中英

Allow empty set in three table join in Spring Data JPA Specification

I am joining three tables in a Spring Data JPA Specification , as such:

public static Specification<Contact> findByStructureCode(final String code) {
    return new Specification<Contact>() {
        @Override
        public Predicate toPredicate(Root<Contact> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
            return criteriaBuilder.equal(root.join(Contact_.contactToStructures)
                    .join(ContactToStructure_.structureCodes)
                    .get(StructureCode_.code), code);
        }
    };
}

However, this is giving me a good old NullPointerException .

java.lang.NullPointerException: null
    at org.hibernate.jpa.criteria.path.AbstractFromImpl.constructJoin(AbstractFromImpl.java:345) ~[hibernate-entitymanager-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.jpa.criteria.path.AbstractFromImpl.join(AbstractFromImpl.java:333) ~[hibernate-entitymanager-4.3.8.Final.jar:4.3.8.Final]
    at org.hibernate.jpa.criteria.path.AbstractFromImpl.join(AbstractFromImpl.java:324) ~[hibernate-entitymanager-4.3.8.Final.jar:4.3.8.Final]
    at my.package.ContactSpecification$3.toPredicate(ContactSpecification.java:83) ~[MyProject-1.0.0.jar:na]

Note that I have replaced package and jar information in that last line with fake information. The line in my code that the stack trace references is .join(ContactToStructure_.structureCodes) .

I believe this is being caused by the fact that not all Contact rows have corresponding ContactToStructure rows in my database.

So, how can I do the first join to allow for Contact entities with an empty contactToStructures set, and just return contacts with non-empty contactToStructures sets?


Here are the relevant pieces of the entities and their metamodels.

Contact.java

@Entity
@Table(name = "CONTACT", schema = "myschema")
public class Contact {    
    @OneToMany(mappedBy = "contact", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
    private Set<ContactToStructure> contactToStructures = new HashSet<>(0);    
}

Contact_.java

@StaticMetamodel(Contact.class)
public class Contact_ {
    public static volatile SetAttribute<Contact, ContactToStructure> contactToStructures;
}

ContactToStructure.java

@Entity
@Table(name = "CONTACT_TO_STRUCTURE", schema = "myschema")
public class ContactToStructure {
    @OneToMany(fetch=FetchType.LAZY, cascade = {CascadeType.ALL}, mappedBy="contactToStructure")
    private Collection<StructureCode> structureCodes = new ArrayList<>();
}

ContactToStructure_.java

@StaticMetamodel(ContactToStructure.class)
public class ContactToStructure_ {
    public static volatile SetAttribute<ContactToStructure, StructureCode> structureCodes;
}

StructureCode.java

@Entity
@Table(name = "STRUCTURE_CODE", schema = "myschema")
public class StructureCode {
    @Column(name = "CODE")
    private String code;
}

StructureCode_.java

@StaticMetamodel(StructureCode.class)
public class StructureCode_ {
    public static volatile SingularAttribute<StructureCode, String> code;
}

If still relevant try to make the following change ContactToStructure_.java :

public static volatile SetAttribute<Version, UseCase> useCases;

To:

public static volatile CollectionAttribute<Version, UseCase> useCases;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM