Search code examples
hibernateembeddable

Mapping exception in hibernate with CollectionOfElements


I am trying to get this mapping to work, but I get this strange exception message

Could not determine type for: foo.ProcessUser, at table: ProcessUser_onetimeCodes, for columns: [org.hibernate.mapping.Column(processUser)]

@Entity
public class ProcessUser {

  @Setter
  private List<OnetimeCodes> onetimeCodes;

  @CollectionOfElements
public List<OnetimeCodes> getOnetimeCodes() {
    return onetimeCodes;
  }
}


@Embeddable
@Data
public class OnetimeCodes {

    @Parent
    private ProcessUser processUser;

    @Column(nullable=false)
    @NotEmpty
    private String password;


    public OnetimeCodes(ProcessUser processUser, String password) {
        this.processUser = processUser;
        this.password = password;
    }
}

Can anyone spot whats wrong here? I have hibernate.hbm2ddl.auto on create


Solution

  • I found the error.

    You cannot have mapping on the attribute in one of the classes, and on getters on the other. They should match.

    So I changed

    @Embeddable
    @Data
    public class OnetimeCodes {
    
        @Parent
        private ProcessUser processUser;
    
        @Column(nullable=false)
        @NotEmpty
        private String password;
    
    
        public OnetimeCodes(ProcessUser processUser, String password) {
            this.processUser = processUser;
            this.password = password;
        }
    }
    

    to

    @Embeddable
    public class OnetimeCodes {
    
        private ProcessUser processUser;
    
        private String password;
    
        public OnetimeCodes(ProcessUser processUser, String password) {
            this.processUser = processUser;
            this.password = password;
        }
    
        @Parent
        public ProcessUser getProcessUser() {
            return processUser;
        }
    
        public void setProcessUser(ProcessUser processUser) {
            this.processUser = processUser;
        }
    
        @Column(nullable=false)
        @NotEmpty
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    }
    

    and viola. Very stupid if you ask me.