JPA2: Можем ли мы использовать несколько @ElementCollection в сущности?

StackOverflow https://stackoverflow.com/questions/9525721

  •  15-11-2019
  •  | 
  •  

Вопрос

Вот убранная версия моего кода:

@Entity
public class Item implements Serializable{

@Id
@GeneratedValue
private long id;

@ElementCollection(fetch=FetchType.EAGER ,targetClass=Cost.class)
@CollectionTable(name="ItemCost", joinColumns = {@JoinColumn(name="itemId")})
private Set<Cost> costs= new HashSet<Cost>();

@ElementCollection(fetch=FetchType.EAGER ,targetClass=ItemLocation.class)
@CollectionTable(name="ItemLocation", joinColumns = {@JoinColumn(name="itemId")})
private Set<ItemLocation> itemLocations;

}
.

Вышеуказанный код допускается?У меня есть два встраиваемых классов стоимости и предмета, которые я использую с @ElementCollection.

Выпуск: Когда я пытаюсь запустить названный запрос

@NamedQuery(name = "Item.findAll", query = "SELECT i FROM Item i")
.

У меня странное поведение.Рекорды во втором элементированииCollections (таблица ItemLccation) удвоиваются (вставлены в таблицу).

Это было полезно?

Решение

What it comes to JPA 2.0, your code is allowed. It is perfectly legal to have more than one collections that are annotated with ElementCollection. Also, it most likely does not have anything to do with problem you have. By the way, to find out is that really your problem, had you tried your code without costs collection?

In which point exactly duplicates in this collection occur first time? If ItemLocation does not define equals&hashcode, duplicates can easily come as result of adding items by yourself.

Possibly you are facing this problem: Primary keys in CollectionTable and chancing type to list and adding @OrderColumn will help.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top