简体   繁体   中英

Spring, Hibernate, JPA, ManyToOne with added where clause or Differentiator column without the inhertiance

For simplicity, here is a smaller version of my model:

@Entity
public class Request {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;



    @Enumerated(EnumType.STRING)
    private EntityType entityType; //Differentiator
    private String entityId; //The identity of the column it refers to.



    private Timestamp date = new Timestamp(System.currentTimeMillis());

    public enum EntityType {
        TypeOfRequestObject, AnotherType, Unknown
    }

}

Now, using JPA I've come up with some Queries that allow me to access the data, but it's ugly and somewhat hardcoded, What I would prefer is something along the lines of a Where Query in the ManyToOne/OneToMany annotations that would also allow cascading.

@ManyToOne(where="entityType='AnotherType'")

Have you guys heard of anything like this? I can appreciate that ManyToOne may not be the correct approach.

I'm using standard Hibernate 5, JPA and Spring Boot, all of which is requested through a Jackson Message Converter.

Thanks

You can use some filters with this filter tou can access to collections using some specific criteria or conditions.

If you give more info about the tecnologies that you are using maybe we can give a more specific answer.

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