2012-07-09 36 views
0

我使用Eclipse和JBOSS工具從MySQL生成hibernate類。 工資表有複合鍵,一個是FK(userId),一個是AUTO_INCREMENT。所以JBOSS Tool生成了Salary和User類,它也生成了SalaryId類。 我已經在用戶類的getSalaries方法中添加了級聯,但是當我試圖保存新用戶時,我總是得到以下異常: org.hibernate.exception.ConstraintViolationException:無法添加或更新子行:外鍵約束失敗如何在JBOSS工具中使用休眠方式在數據庫中添加新記錄?

有沒有人有任何想法來幫助我解決這個問題?感謝所有〜

下面是我的表結構:

CREATE TABLE IF NOT EXISTS `salary` (
`id` int(11) NOT NULL AUTO_INCREMENT, 
`userId` int(11) NOT NULL, 
`amount` int(11) NOT NULL, 
`comingDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 
PRIMARY KEY (`id`,`userId`), 
KEY `userId` (`userId`) 
) 

下面是我生成的類: [User類]

@Entity 
@Table(name = "user", catalog = "lab") 
public class User implements java.io.Serializable { 

    private Integer id; 
    private String name; 
    private String password; 
    private String email; 
    private Set<Salary> salaries = new HashSet<Salary>(0); 

    public User() { 
    } 

    public User(String name, String password, String email, Set<Salary> salaries) { 
     this.name = name; 
     this.password = password; 
     this.email = email; 
     this.salaries = salaries; 
    } 

    @Id 
    @GeneratedValue(strategy = IDENTITY) 
    @Column(name = "id", unique = true, nullable = false) 
    public Integer getId() { 
     return this.id; 
    } 
    public void setId(Integer id) { 
     this.id = id; 
    } 

    @Column(name = "name", nullable = false, length = 100) 
    public String getName() { 
     return this.name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 

    @Column(name = "password", nullable = false, length = 100) 
    public String getPassword() { 
     return this.password; 
    } 
    public void setPassword(String password) { 
     this.password = password; 
    } 

    @Column(name = "email", nullable = false, length = 50) 
    public String getEmail() { 
     return this.email; 
    } 
    public void setEmail(String email) { 
     this.email = email; 
    } 

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "user", cascade = CascadeType.ALL) 
    public Set<Salary> getSalaries() { 
     return this.salaries; 
    } 
    public void setSalaries(Set<Salary> salaries) { 
     this.salaries = salaries; 
    } 
} 

[工資類]

@Entity 
@Table(name = "salary", catalog = "lab") 
public class Salary implements java.io.Serializable { 

    private SalaryId id; 
    private User user; 
    private int amount; 
    private Date comingDate; 

    public Salary() { 
    } 

public Salary(SalaryId id, User user, int amount) { 
     this.id = id; 
     this.user = user; 
     this.amount = amount; 
    } 

    public Salary(SalaryId id, User user, int amount, Date comingDate) { 
     this.id = id; 
     this.user = user; 
     this.amount = amount; 
     this.comingDate = comingDate; 
    } 

    @EmbeddedId 
    @AttributeOverrides({ 
      @AttributeOverride(name = "id", column = @Column(name = "id", nullable = false)), 
      @AttributeOverride(name = "userId", column = @Column(name = "userId", nullable = false)) }) 
    public SalaryId getId() { 
     return this.id; 
    } 

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

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "userId", nullable = false, insertable = false, updatable = false) 
    public User getUser() { 
     return this.user; 
    } 

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

    @Column(name = "amount", nullable = false) 
    public int getAmount() { 
     return this.amount; 
    } 

    public void setAmount(int amount) { 
     this.amount = amount; 
    } 

    @Temporal(TemporalType.TIMESTAMP) 
    @Column(name = "comingDate", nullable = false, length = 19) 
    public Date getComingDate() { 
     return this.comingDate; 
    } 

    public void setComingDate(Date comingDate) { 
     this.comingDate = comingDate; 
    } 
} 

以下是由JBOSS Tool自動生成的:

[SalaryId類]

@Embeddable 
public class SalaryId implements java.io.Serializable { 

    private int id; 
    private int userId; 

    public SalaryId() { 
    } 

    public SalaryId(int id, int userId) { 
     this.id = id; 
     this.userId = userId; 
    } 

    @Column(name = "id", nullable = false) 
    public int getId() { 
     return this.id; 
    } 

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

    @Column(name = "userId", nullable = false) 
    public int getUserId() { 
     return this.userId; 
    } 

    public void setUserId(int userId) { 
     this.userId = userId; 
    } 

    public boolean equals(Object other) { 
     if ((this == other)) 
      return true; 
     if ((other == null)) 
      return false; 
     if (!(other instanceof SalaryId)) 
      return false; 
     SalaryId castOther = (SalaryId) other; 

     return (this.getId() == castOther.getId()) 
       && (this.getUserId() == castOther.getUserId()); 
    } 

    public int hashCode() { 
     int result = 17; 

     result = 37 * result + this.getId(); 
     result = 37 * result + this.getUserId(); 
     return result; 
    } 
} 

[主類]

transaction = session.beginTransaction(); 
SalaryId salaryId = new SalaryId(); 
Set<Salary> salaries = new HashSet<Salary>(); 

User user = new User(); 
user.setName("JW"); 
user.setPassword("123456"); 
user.setEmail("[email protected]"); 

salaries.add(new Salary(salaryId, user, 10000)); 
user.setSalaries(salaries); 

session.save(user); 

transaction.commit(); 

回答

0

嘗試做這樣的:

transaction = session.beginTransaction(); 
SalaryId salaryId = new SalaryId(); 
Set<Salary> salaries = new HashSet<Salary>(); 

User user = new User(); 
user.setName("JW"); 
user.setPassword("123456"); 
user.setEmail("[email protected]"); 

session.save(user); 

salaries.add(new Salary(salaryId, user, 10000)); 
//user.setSalaries(salaries); 

transaction.commit();