我想使用map作为类的属性,使用id作为键以便于检索。但是,我不清楚如何将新实体添加到地图中,因为新实体将没有id(id == null)。它可以使用一个新对象+级联注释,但有时会添加几个新对象。
有人知道如何解决这个问题吗?
@Entity
@Table(name = "Sources")
@Lazy(false)
public class Sources {
@ManyToMany( fetch = FetchType.EAGER )
@JoinTable( name = "sources_lists", joinColumns = @JoinColumn( name = "list_id" ) )
@MapKeyColumn( name = "source_id" )
public Map<Integer, Source> getSources() { return sources; }
public void setSources( Map<Integer, Source> sourcesList ) { this.adSources = adSourcesList; }
private Map<Integer, Source> sources;
@Override
@Id @GeneratedValue(strategy = GenerationType.AUTO)
@Column( name="id", unique = true, nullable = false, updatable = false )
public Integer getId() { return super.getId(); }
}