0

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]
2
  • @ElementCollection is 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 add oldPassword to your list! I don't know if I'm getting your idea? Commented Oct 21, 2017 at 1:03
  • Generally, I wanted something what @Andriy Slobodyanyk wrote so accepted the answer. There is saving whole list and I just wanted to get one (the earliest) value and make insert with it but it is oke, I made trigger on table and when the new value is comming then the earliest are deleted. Commented Oct 23, 2017 at 6:14

2 Answers 2

1
+50

As you described at the first sentence you have one-to-many relationship

@ElementCollection
@CollectionTable(name="hist_bo_operator_password", joinColumns=@JoinColumn(name="id_operator"))
@Column(name="password")
@OrderBy("data_ins")
public List<String> oldPasswords = new ArrayList<String>();

Then add necessary getter

Optional<String> getPassword() {
    return oldPasswords.size() > 0
        ? Optional.of(oldPasswords.get(0))
        : Optional.empty();
}

And setter

void setPassword(String password) { // or maybe addPassword?
    oldPasswords.add(password);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Okey, that's how I can get the last password but I would also set this oldPassword to use save on Operator object later. The goal is to make an insert to hist_bo_operator_password this code would save the whole list.
@Michu93, well, add setter, modified the code above
That's close what I expect but it is saving the whole list instead of just one value. I think there is needed some limit or annotation to oldPassword. Now we are trying to do everything on list.
1

Why don't you create entity of hist_bo_operator_password? Then you could have List of that entities (instead of just String List) and just add another object to List and save entity Operator.

1 Comment

I just need one column from hist_bo_operator_password so I think new @Entity for that is not needed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.