Tessaiga
Tessaiga

Reputation: 11

Possible to retrieve selected children list from database only by using JPA/Hibernate-Annotation?

Guess this might be a difficult one. Well, at least for me. I need some help with JPA/Hibernate-Annotations and I was wondering, if there is a way to solve my problem. Unfortunately, my own skills seem too limited to solve that problem by myself. That's why I'm hoping to find anyone here who might have a good grasp on jpa-annotations and relational databases (here: oracle).

Task:

I have three tables (see attached image! Please don't change this part, since the tables aren't supposed to be created! They already exist with exactly these names, may not be changed and the primary key + foreign key part is just to provide more information about these already existing tables that I uploaded here as an image! Thank you.):

CONSTANT (PRIMARY KEY CONSTANT(ID) + FOREIGN KEY (PARENT_CONSTANT_ID) REFERENCES CONSTANT(ID)),
COUNTRY (PRIMARY KEY COUNTRY(ID)),
COUNTRY_TO_CONSTANT (PRIMARY KEY COUNTRY_TO_CONSTANT(COUNTRY_ID, CONSTANT_ID))

as shown here: Tables-Image

Java-Beans look like that:

Constant.java

@Id
@Column(name = "ID")
private Integer id;

@Basic
@Column(name = "CODE")
private String code;

@Basic
@Column(name = "SORT_KEY")
private int sortKey;

@ManyToOne
@JoinColumn(name = "CONSTANT_TYPE_ID", referencedColumnName = "ID")
private ConstantType constantType;

@ManyToOne
@JoinColumn(name = "parent_constant_id", referencedColumnName = "ID")
private Constant parent;

@OneToMany(mappedBy = "parent", targetEntity = Constant.class)
private List children;

...
    
public Collection<Constant> getChildren() {
    return children;
}

CountryToConstant.java

@Id
@Basic
@Column( name = "COUNTRY_ID" )
private Integer countryID;

@Id
@Basic
@Column( name = "CONSTANT_ID" )
private Integer constantID;

Country.java

@Id
@Column( name = "ID" )
@SequenceGenerator( name = "CountrySequence", sequenceName = "COUNTRY_ID_SEQ" )
@GeneratedValue( generator = "CountrySequence" )
private Integer id;
    
@Basic
@Column( name = "CODE" )
private String code;
    
@Basic
@Column( name = "NAME" )
private String name;
    
@Basic
@Column( name = "LANGUAGE_CODES" )
private String languageCodes;

I left out all getters + setters for brevity except for the constant.java-getter of the 'children'-attribute, since this is the only getter needed for this issue. To make it more difficult: The structure of the java-beans shown above may not be changed, but is allowed to be expanded. This is due to not destroying the already existing (very complex) programm structure.

private List children (Constant.java) is mapped by the attribute private Constant parent. Let's assume that by the other program logic a list with 'parent' CONSTANT ids (e.g. id = {11, 12}) is used here to iterate through and handed over one after another to create 1 parent-object. As a result this will create one private List children after another. Therefore, the result of the example would be 2 separate children lists due to the iteration:

  1. parent-id 11 -> children-id-list: {111, 112, 113, 114, 115, 116}
  2. id 12 -> id-list: {121, 122, 123}) for each parent-object.

What I need to get instead is a specific list corresponding to the COUNTRY_TO_CONSTANT table, only by using annotations. For example: If the person uses a browser in Germany, the locale is set to de_DE resulting in the COUNTRY.id '1001'. And based (only) on the country code (not the locale), the result for the iteration example above (CONSTANT.id = {11, 12}) should be these two lists:

  1. parent-id 11 -> children-id-list: {111, 112, 113} (leaving out constant-id 114, 115, and 116)
  2. parent-id 12 -> children-id-list: {121} (leaving out constant-id 122, and 123)

Idea is that depending on the country code and the CONSTANT.IDs put in the table COUNTRY_TO_CONSTANT, every list is created individually corresponding to the related database entries.

Since private List children cannot be changed in definition, what I need is a second attribute (e.g. private List children2) that works exactly like private List children with the only difference that only the country related children (see COUNTRY_TO_CONSTANT table) should be mapped by private Constant parent.

I was thinking about something like:

@OneToMany(mappedBy = "parent", targetEntity = Constant.class)
//Any annotation to get what I need.
private List children2

or maybe:

@ManyToMany(mappedBy = "children", targetEntity = Constant.class)
//Any annotation to get what I need.
private Set children2

Is that even possible to achieve just by using annotations?

Hope, things are clearly explained so far. If not, what kind of workaround could I use here? If any info is missing, please let me know.

Thanks a lot.

Upvotes: 1

Views: 76

Answers (0)

Related Questions