我已经在这方面工作了一段时间,似乎无法弄清楚,除了在某些时候它按预期工作。我有两个对象,一个可以与多个地址关联的用户。保存新用户和地址一起工作正常,但当我尝试向现有用户添加新地址时,我收到有关用户表的重复主键的错误。我想这与hibernate中的配置有关,尝试单向和双向。
以下是任何相关文件。谢谢你的帮助!
User.java
@Entity
@Table(name = "users")
public class Users implements Serializable {
private static final long serialVersionUID = -5699491123012997265L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int idusers;
@NotBlank(groups = { PersistenceValidationGroup.class, FormValidationGroup.class })
@Size(min = 5, max = 15, groups = { PersistenceValidationGroup.class, FormValidationGroup.class })
@Pattern(regexp = "^\\w{5,}$", groups = { PersistenceValidationGroup.class, FormValidationGroup.class })
private String username;
@NotBlank(groups = { PersistenceValidationGroup.class, FormValidationGroup.class })
@Pattern(regexp = "^\\S+$", groups = { PersistenceValidationGroup.class, FormValidationGroup.class })
@Size(min = 8, max = 15, groups = { FormValidationGroup.class })
private String password;
private boolean enabled = false;
private String authority;
@Size(max = 25, groups = { PersistenceValidationGroup.class, FormValidationGroup.class })
private String name;
@Size(min = 10, max = 10, groups = { PersistenceValidationGroup.class, FormValidationGroup.class })
private String phoneNo;
@OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
@JoinColumn(name = "idaddresses")
private Set<Addresses> addresses;
// CONSTRUCTORS
public Users() {
}
public Users(String username, String password, boolean enabled,
String authority, String name, String phoneNo,
Set<Addresses> addresses) {
this.username = username;
this.password = password;
this.enabled = enabled;
this.authority = authority;
this.name = name;
this.phoneNo = phoneNo;
this.addresses = addresses;
}
// GETTERS AND SETTERS
public int getIdUsers() {
return idusers;
}
public void setIdusers(int idusers) {
this.idusers = idusers;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public String getAuthority() {
return authority;
}
public void setAuthority(String authority) {
this.authority = authority;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIdusers() {
return idusers;
}
public String getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
public Set<Addresses> getAddresses() {
return addresses;
}
public void setAddresses(Set<Addresses> addresses) {
this.addresses = addresses;
}
public void addAddress(Addresses addresses) {
if (this.addresses == null) {
this.addresses = new HashSet<Addresses>();
}
addresses.setUsers(this);
this.addresses.add(addresses);
}
}
Addresses.java
@Entity
@Table(name = "addresses")
public class Addresses implements Serializable {
private static final long serialVersionUID = -1239830513656245466L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int idaddresses;
@NotBlank
@Size(min = 5, max = 45)
private String street1;
@NotBlank
@Size(min = 5, max = 45)
private String street2;
@Size(min = 0, max = 200)
private String special;
private String apt;
private String city;
private String state;
@ManyToOne
@JoinColumn(name = "idusers")
private Users users;
// CONSTRUCTORS
public Addresses() {
}
public Addresses(String street1, String street2, String special,
String apt, String city, String state, Users users) {
this.street1 = street1;
this.street2 = street2;
this.special = special;
this.apt = apt;
this.city = city;
this.state = state;
this.users = users;
}
// GETTERS AND SETTERS
public int getIdaddresses() {
return idaddresses;
}
public void setIdaddresses(int idaddresses) {
this.idaddresses = idaddresses;
}
public String getStreet1() {
return street1;
}
public void setStreet1(String street1) {
this.street1 = street1;
}
public String getStreet2() {
return street2;
}
public void setStreet2(String street2) {
this.street2 = street2;
}
public String getSpecial() {
return special;
}
public void setSpecial(String special) {
this.special = special;
}
public String getApt() {
return apt;
}
public void setApt(String apt) {
this.apt = apt;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
}
调用方法
的部分流程<view-state id="newAddress" model="address">
<transition on="addNewAddress" to="customerReady" />
<on-exit>
<evaluate expression="address.street1 = requestParameters.street1"></evaluate>
<evaluate expression="address.street2 = requestParameters.street2"></evaluate>
<evaluate expression="address.apt = requestParameters.apt"></evaluate>
<evaluate expression="address.city = requestParameters.city"></evaluate>
<evaluate expression="address.special = requestParameters.special"></evaluate>
<evaluate expression="address.city = requestParameters.city"></evaluate>
<evaluate expression="address.state = 'NY'"></evaluate>
<evaluate expression="user.addAddress(address)"></evaluate>
<evaluate expression="usersService.update(user)"></evaluate>
</on-exit>
</view-state>
userService.update(user)将您发送到dao
public void update(Users users) {
session().saveOrUpdate(users);
}