简体   繁体   中英

JPA hibernate one to many relationship creates extra column name

I have two entities. I want to cascade the insertion of the child entity when the owner entity is persisted and set the SSO_ID of the child entity to the one that was generated for the owner by the generator.

@Entity(name = "USERS")
@Table(name = "USERS")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ID_GENERATOR")
    @SequenceGenerator(name = "ID_GENERATOR", sequenceName = "ID_SEQUENCE")
    @Column(name = "SSO_ID")
    private Long ssoId;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    private List<UserEmail> userEmails = new ArrayList<>();

    // getters, setters etc.
}

@Entity(name = "USER_EMAILS")
@Table(name = "USER_EMAILS")
@IdClass(UserEmailId.class)
public class UserEmail {

    @Id
    @Column(name = "SSO_ID")
    private Long ssoId;

    @Id
    @Column(name = "USER_MAIL")
    private String userMail;

    @Id
    @Column(name = "START_DATE")
    private Date startDate;

    @Id
    @Column(name = "EMAIL_TYPE")
    private String emailType;

    @ManyToOne(fetch = FetchType.LAZY)
    private User user;

    // getters, setters etc.
}

The UserEmail ID class is:

public class UserEmailId implements Serializable {

    private Long ssoId;
    private String userMail;
    private Date startDate;
    private String emailType;

    // getters, setters etc.
}

Instead, I get an error:

insert into hub_users_emails (user_sso_id, email_type, sso_id, start_date, user_mail) values (?, ?, ?, ?, ?)

(etc.)

binding parameter [1] as [BIGINT] - [1234837655] => this is user_sso_id

(etc.)

binding parameter [3] as [BIGINT] - [null] => this is the original sso_id

SQL Error: 904, SQLState: 42000

ohengine.jdbc.spi.SqlExceptionHelper : ORA-00904: "USER_SSO_ID": invalid identifier

I've tried some other setups of one to many (bidirectional, unidirectional, etc.) but It seems that this problem persists between all implementations.

Help is appreciated.

As you use @ManyToOne and @OneToMany, hibernate will create user_sso_id on your USER_EMAILS table. I am not sure why do you want another ssoId on USER_EMAILS. I have removed sso_id from USER_EMAILS and now it's working fine. I know this is not the exact answer of your question. Following code may help you.

@Entity(name = "USERS")
@Table(name = "USERS")
@Setter
@Getter
public class User {

 @Id
 @SequenceGenerator(name = "ID_GENERATOR", sequenceName = "ID_SEQUENCE")
 @GeneratedValue(generator = "ID_GENERATOR" )
 @Column(name = "SSO_ID")
 private Long ssoId;

 @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "user")
 private List<UserEmail> userEmails = new ArrayList<>();

}



@Setter
@Getter
@Entity(name = "USER_EMAILS")
@Table(name = "USER_EMAILS")
@IdClass(UserEmailId.class)
public class UserEmail {


 @Id
 @Column(name = "USER_MAIL")
 private String userMail;

 @Id
 @Column(name = "START_DATE")
 private Date startDate;

 @Id
 @Column(name = "EMAIL_TYPE")
 private String emailType;

 @ManyToOne(fetch = FetchType.LAZY)
 private User user;

}


@Setter
@Getter
public class UserEmailId implements Serializable {

  private String userMail;
  private Date startDate;
  private String emailType;

}


public class SomeClass{ 

 public User saveUser(){
     User user = new User();
     UserEmail userEmail = new UserEmail();
     userEmail.setUser(user);
     userEmail.setEmailType("type");
     userEmail.setStartDate(new Date());
     userEmail.setUserMail("someEmail@gmail.com");
     user.setUserEmails(Arrays.asList(userEmail));
     userRepo.save(user);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM