2012-07-09 5 views
0

Eclipse와 JBOSS 도구를 사용하여 MySQL에서 최대 절전 모드 클래스를 생성하고 있습니다. 급여 테이블에 복합 키가 있습니다. 하나는 FK (userId)이고, 하나는 AUTO_INCREMENT입니다. 그래서 JBOSS Tool은 Salary와 User 클래스를 생성했고 SalaryId 클래스도 생성했습니다. User 클래스의 getSalaries 메소드에서 캐스케이드를 추가했으나 새 사용자를 저장하려고 할 때 항상 예외가 발생합니다. org.hibernate.exception.ConstraintViolationException : 자식 행을 추가하거나 업데이트 할 수 없습니다 : 외래 키 제약 조건 실패JBoss Tools에서 최대 절전 모드를 사용하여 데이터베이스에 새로운 레코드를 추가하는 방법은 무엇입니까?

이 문제를 해결할 수있는 사람이 있습니까? 다음은 모두 감사합니다 ~

내 테이블 구조입니다 :

다음
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`) 
) 

있는 내 생성 된 클래스 : [사용자 클래스]

@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 도구에 의해 자동 생성 됨 :

,210

[대하여 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(); 
관련 문제