0

spring3 mvc で hibernate 3 アノテーションを使用して、2 つのテーブルで単純な結合操作を実装しようとしています。

私は2つのテーブルを持っています:

従業員

CREATE TABLE IF NOT EXISTS `employee` (`enter code here`
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `address1` varchar(100) NOT NULL,
  `address2` varchar(100) NOT NULL,
  `created_at` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB

給料

CREATE TABLE IF NOT EXISTS `salary` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `employee_id` int(11) NOT NULL,
  `basic_pay` int(5) NOT NULL,
  `take_home` int(5) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `employee_id` (`employee_id`)
) ENGINE=InnoDB

2 つの注釈付きモーダル クラスを作成しました。

従業員.java

@Entity
@Table(name = "employee", catalog = "employee")
public class Employee implements java.io.Serializable {

    private Integer id;
    private String name;
    private String address1;
    private String address2;
    private Date createdAt;
    private Set salaries = new HashSet(0);

    public Employee() {
    }

    public Employee(String name, String address1, String address2,
            Date createdAt) {
        this.name = name;
        this.address1 = address1;
        this.address2 = address2;
        this.createdAt = createdAt;
    }

    public Employee(String name, String address1, String address2,
            Date createdAt, Set salaries) {
        this.name = name;
        this.address1 = address1;
        this.address2 = address2;
        this.createdAt = createdAt;
        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 = "address1", nullable = false, length = 100)
    public String getAddress1() {
        return this.address1;
    }

    public void setAddress1(String address1) {
        this.address1 = address1;
    }

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

    public void setAddress2(String address2) {
        this.address2 = address2;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_at", nullable = false, length = 0)
    public Date getCreatedAt() {
        return this.createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "employee")
    public Set getSalaries() {
        return this.salaries;
    }

    public void setSalaries(Set salaries) {
        this.salaries = salaries;
    }

Salary.java

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

    private Integer id;
    private Employee employee;
    private int basicPay;
    private int takeHome;

    public Salary() {
    }

    public Salary(Employee employee, int basicPay, int takeHome) {
        this.employee = employee;
        this.basicPay = basicPay;
        this.takeHome = takeHome;
    }

    @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;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "employee_id", nullable = false)
    public Employee getEmployee() {
        return this.employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    @Column(name = "basic_pay", nullable = false)
    public int getBasicPay() {
        return this.basicPay;
    }

    public void setBasicPay(int basicPay) {
        this.basicPay = basicPay;
    }

    @Column(name = "take_home", nullable = false)
    public int getTakeHome() {
        return this.takeHome;
    }

    public void setTakeHome(int takeHome) {
        this.takeHome = takeHome;
    }

ページを開くと、次のエラーが発生しました

org.springframework.beans.factory.BeanCreationException: Error creating bean 
with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: 
Invocation of init method failed; nested exception is org.hibernate.MappingException: 
Could not determine type for: java.util.Set, at table: EMPLOYEE, for columns: 
[org.hibernate.mapping.Column(salaries)]

私のモーダルクラスに何か問題があります

ヘルプは大歓迎です、ありがとう、VKS

4

2 に答える 2

2

あなたSetの給与は生のタイプであり、休止状態はそれを1つのエンティティで「マップ」する方法を知りません。あなたに追加targetEntityする@oneToManyか、使用してみてくださいSet<Salary>

于 2011-10-24T05:32:58.897 に答える
1

私は専門家ではありませんが、SET のタイプを hibernate に伝える必要はありません。この場合、hibernate SET は 1 対多の関係を表しているように見えますが、どのテーブルと??

*.hbm.xml マッピング ファイルでは、"table" 属性がこの目的で使用されたと思います。

例えば

<set cascade="persist, merge, save-update, evict, replicate, lock, refresh" name="Salaries" inverse="true" lazy="true" table="salary">
            <key>
               //code goes here
            </key>
            <one-to-many class="Salary" />
        </set>
于 2011-10-24T05:31:08.850 に答える