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에서 사용하고있는 두 개의 임베디드 클래스 비용과 ItemLocation이 있습니다.

문제 : 이름이 지정된 쿼리를 실행하려고하면

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

나는 이상한 행동이 있습니다.두 번째 엘리먼트 콜렉션 (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