S
S
ScRsa2016-02-15 09:32:20
Java
ScRsa, 2016-02-15 09:32:20

How to correctly define Entity if part of the data is stored in the properties table?

There is an entity, part of whose data is stored in a regular table, and the other in a property table (PK - Attribute_Name - Attribute_Value). The set of properties may differ for different types of this entity.

@Entity
@Table(name = "PEOPLE")
public class People {
  @Id	
  private int codePeople;
 
  @Column(name = "FAM")
  private String fam;
 
  @Column(name = "IM")
  private String im;
 
  @Column(name = "OTCH")
  private String otch;

  ...
}

// Свойства
@Table(name = "PEOPLE_ATTRIBUTE")
@Column(name = "Code_People")
@Column(name = "Code_Attr")
@Column(name = "Value_Attr")

// Cобственно: В зависимости от региона адрес человека хранится в разных видах
// 1 - Полное название (Регион, Город, Улица, Дом...)
// 2 - Региональные Код региона Код улицы Дом (то есть несколько строк в таблице свойств)
// 3 - Код КЛАДР
// 4 - Код ФИАС
// И таких различных атрибутов около сотни

The question is how to build an Entity based on this structure?
I look in the direction of @SecondaryTable, but, in my opinion, this is not it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sirs, 2016-02-15
@ScRsa

As one of the options - you can do it with the @OneToMany annotation. Create a separate entity on the PEOPLE_ATTRIBUTE table, for example

@Entity
@Table(name = "PEOPLE_ATTRIBUTE")
public class PeopleAttribute {
private int peopleCode;
private int attributeCode;
private String attributeValue;
...
}

@Entity
@Table(name = "PEOPLE")
public class People {

...
private Set<PeopleAttribute> attributes;

@OneToMany(mappedBy = "attribute")
public Set<PeopleAttribute> getAttributes() {
    return this.attributes;
  }
...
}

PS A little advice from experience: 1) always use Long for entity id, even if it seems to you that short / int is enough for you, the overhead is not so big, you should not save on matches. 2) If you have @Id private int codePeople; is an ID, then call it peopleId, peopleID, or just id. When there are a lot of entities, confusion and problems begin, especially if there are a lot of people in the team and everyone writes differently.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question