OneToOne ids生成错误

时间:2015-08-29 13:54:43

标签: java spring hibernate jpa

我有这个错误嵌套异常是org.hibernate.id.IdentifierGenerationException:在调用save()之前必须手动分配此类的ID:app.Spring.domain.UserDetails。

我现在有了这个用户表,在配置文件中我想编辑这个UserDetails。 我正在尝试使用GeneratedValue,但这个做与user_id无关的随机id也检查了生成器,但这种方法也不起作用。有很多选项,所以我现在迷失了。可以有人展示一些方法来映射这两个实体吗?

用户

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

 private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
@Column(name = "user_id")
private Long user_id;

@NotBlank
@Size(min = 5, max = 20)
private String username;

@NotBlank
@Size(min = 8, max = 20)
private String password;

private String email;

private String name;

private String surname;

@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private UserDetails userDetail;

public User() {

}

public User(Long user_id, String username, String email, String name,
        String surname, UserDetails userDetail, String password) {
    super();
    this.user_id = user_id;
    this.username = username;
    this.email = email;
    this.name = name;
    this.surname = surname;
    this.userDetail = userDetail;
    this.password = password;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}

public Long getUser_id() {
    return user_id;
}

public final void setUser_id(Long user_id) {
    this.user_id = user_id;
}



public void setId(Long user_id) {
    this.user_id = user_id;
}

@Column(name = "username")
public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

@Column(name = "password")
public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

    }

User_Details

@Entity
@Table(name = "user_address")
public class UserDetails {


  public UserDetails() {
    super();
    // TODO Auto-generated constructor stub
}

  @Id
  @Column(name = "user_id")
  private Long id;


  private String adres1;

  private String adres2;

  private String city;

  private String postcode;


   @OneToOne
   @PrimaryKeyJoinColumn
  private User user;



public UserDetails(Long id, String adres1, String adres2, String city,
        String postcode, User user) {
    super();
    this.id = id;
    this.adres1 = adres1;
    this.adres2 = adres2;
    this.city = city;
    this.postcode = postcode;
    this.user = user;
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getAdres1() {
    return adres1;
}

public void setAdres1(String adres1) {
    this.adres1 = adres1;
}

public String getAdres2() {
    return adres2;
}

public void setAdres2(String adres2) {
    this.adres2 = adres2;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getPostcode() {
    return postcode;
}

public void setPostcode(String postcode) {
    this.postcode = postcode;
}

public void setId(Long id) {
    this.id = id;
}

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}


    }

LOGIC

@RequestMapping(value = "/userDetails", method = RequestMethod.GET)
    public String showForm(Model model,
        @RequestParam(value = "id", defaultValue = "-1") Long id,
        HttpSession session) {

        app.Spring.domain.UserDetails va = (id > 0) ? reg.getAdress(id)
                : new UserDetails();


        model.addAttribute("detal", va);

        return "userDetails";
    }

    @RequestMapping(value = "/userDetails", method = RequestMethod.POST)
    public String submit(Model model, @ModelAttribute("detal") UserDetails va,
            BindingResult result) {

        validator.validate(va, result);

        if (result.hasErrors()) {
            return "userDetails";
        }

        reg.saveOrUpdateUserDetails(va);

        return "profile";
    }

2 个答案:

答案 0 :(得分:0)

我不知道如何使用注释执行此操作,但您可能会尝试在{{1}的UserDetails实体中手动分配从id获取的user {希望已经存在的} @PrePresist注释方法。

答案 1 :(得分:0)

用户

CSS

<强> User_detail

 @Entity
    @Table(name = "USERS")
    public class User implements Serializable {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        @Id
        @GeneratedValue
        @Column(name = "user_id")
        private Long user_id;

        @NotBlank
        @Size(min = 5, max = 20)
        private String username;

        @NotBlank
        @Size(min = 8, max = 20)
        private String password;

        private String email;

        private String name;

        private String surname;

        @OneToOne(mappedBy = "user")
        private UserDetails userDetail;