2 объекта JPA на той же таблице

Я использую автосвойства все время. Перед C#3 я не мог быть побеспокоен всем вводом и просто используемыми общедоступными переменными вместо этого.

единственной вещью, которую я пропускаю, является способность сделать это:

public string Name = "DefaultName";

необходимо сместить значения по умолчанию в конструкторов со свойствами. утомительный:-(

26
задан Manuel Drieschmanns 7 November 2017 в 21:04
поделиться

1 ответ

The most straightforward way to do this is to map properties you don't use often as lazy:

<property name="extendedProperty" lazy="true" />

... or using Annotations ...

@Basic(fetch = FetchType.LAZY)
String getExtendedProperty() { ... }

Hibernate would not load such properties initially; instead they'll be loaded on demand (when first accessed). You can force Hibernate to load all properties by using fetch all properties clause in your HQL query.

Another possible scenario is to actually map two completely separate entities to the same table but make one of them immutable. Keep in mind that they will be treated as different entities by Hibernate, with first / second level cache being completely separate for both (which is why immutability is important).

You will NOT be able to achieve this functionality via inheritance mapping because Hibernate always returns an actual concrete entity type. Take a look at my answer to Hibernate Inheritance Strategy question for a detailed explanation.

17
ответ дан 28 November 2019 в 17:25
поделиться
Другие вопросы по тегам:

Похожие вопросы: