开发者

Complex JPA relationship

I need help to build a complex relationship among the following tables:

Complex JPA relationship

Here is the scenario: 1) 1 Family has N familyTypes (unidirectional) 2) 1 Family has N familyFields (Unidirectional)

EDIT: The first requirement is a family "A" has a certain list of family fields which can be used by the family_types. For example family "A" has 4 fields out of 20 fields of Family_Fields. Now,each member of family_type can use and configure only these 4 family fields,not others. Also, now each Family has a certain number of family types. At the end I want to have isvisible property on a family field of family type of family.

My problem is, I want to have another entity (join of FamilyType+FamilyField) where one family field belonging to to a particular family_type but has 1 more additional field (isVisible).

So the excerpt is, family x has y type and y has a,b family_fields where a is visible and b is not visible for type y.

I have tried to get the join_table and make that an entity class with the additional field. But it looks a mess.

Is there any other way to do that? So that I do like this:

Family f = getFamily("X");
FamilyTypes ft = f.getTypes();
FamilyFields ff = f.getFields(); 
SomeEntity = ft.getFieldConfiguarations(); // problem is hwo to do this

I can not put that additional field in FamilyType as many other FamilyType can use that field

I am getting confused :(


Ok, after some try I cam up with the following class:

@Entity
@IdClass(FieldConfiguarationPk.class)
public class FieldConfiguaration implements Serializable{

@Id
@Column(name = "FAMILY_ID")
private Long familyId;

@Id
@Column(name = "FAMILY_TYPE_ID")
private Long familyTypeId;


@Id
@Column(name = "FAMILY_FIELD_ID")
private Long familyFieldId;


@Column(name = "REQUIRED")
private boolean isRequired;

@Column(name = "HELP")
private boolean isHelpAvailable;


@ManyToOne
@JoinColumn(name = "FAMILY_ID", insertable = false, updatable = false, referencedColu开发者_开发百科mnName ="Id")
private Family family;

@ManyToOne
@JoinColumn(name = "FAMILY_TYPE_ID", insertable = false, updatable = false, referencedColumnName = "Id")
private FamilyType familyType;

@ManyToOne
@JoinColumn(name = "FAMILY_FIELD_ID", insertable = false, updatable = false, referencedColumnName = "Id")
private FamilyField familyField;
/*getter,setter*/

But my problem is only the foreign key with FAMILY_TYPE_ID is being created.not the other twos. I also have @OneToMany on the other table which is mappedBy this tables corresponding fields.

Here is the schema definition:

Complex JPA relationship

WHY THE OTHER TWO FOREIGN KEYS ARE NOT GENERATED? ANY IDEA?


Here's an answer, but I don't know if it will help you as it depends if you can use it in your domain (this might make no sense in your domain).

I think the solution would be to create a ternary association between Family, FamilyType and FamilyField, and this association table would have the "isVisible" column.

So you would have 3 tables with just an id and name columns (Family, FamilyType and FamilyField) and another table with the following fields

  • id
  • family_id
  • family_type_id
  • family_field_id
  • is_visible

You could map this table in a different way and use a composite key, but I think that might complicate the solution.

In this way, you also ensure that for each FamilyField associated to a Family, there's a FamilyType. A good exercise would be if you can find a representative name for this entity/table, as it should represent something in the domain.

To simplify navigation between objects you could add methods on Family that retrieves only the fields or types.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜