I have two tables bo_operator and hist_bo_operator_password. In bo_operator the id column is foreign key to hist_bo_operator_password and I can have many the same operator_id in hist_bo_operator_password and only one id in bo_operator.
My entity:
@Entity
@Table(name="bo_operator")
public class Operator implements Serializable
and that's how I am getting values from hist_bo_operator_password:
@ElementCollection
@CollectionTable(name="hist_bo_operator_password", joinColumns=@JoinColumn(name="id_operator"))
@Column(name="password")
public List<String> oldPasswords = new ArrayList<String>();
but when I'm trying to get only one value by:
@ElementCollection
@CollectionTable(name="hist_bo_operator_password", joinColumns=@JoinColumn(name="id_operator"))
@Column(name="password")
public String oldPassword;
I'm getting error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: local.vlex.operator.model.Operator.oldPassword
and all I want to do is makeing an insert into hist_bo_operator_password by
operator.setOldPassword(oldPassword);. I think the problem is that it doesn't know which password take if there is many values for the same id.
How to achive it?
@Edit I also tried:
@Table(name="bo_operator")
@SecondaryTable(name = "hist_bo_operator_password",pkJoinColumns=@PrimaryKeyJoinColumn(name="id_operator", referencedColumnName="id"))
I even found ORDER BY so:
@Column(name="password", table="hist_bo_operator_password")
@OrderBy("data_ins")
public String oldPassword;
but seems like there is no @Limit or something like this in JPA and I still have many values to the same id which cause error:
org.hibernate.HibernateException: Duplicate identifier in table for: [local.vlex.operator.model.Operator#1]
@ElementCollectionis used for collection, not for single value type. Btw why do you want to get only one value (while you're using a collection)? and you can make an insert by just addoldPasswordto your list! I don't know if I'm getting your idea?