Hibernate注解连接问题

发布于 2024-12-11 23:00:52 字数 4782 浏览 5 评论 0原文

我正在尝试使用 hibernate 3 注释和 spring3 mvc 来实现两个表的简单连接操作。

我有两个表:

Employee

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

Salary

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

我创建了两个带注释的模态类:

Employee.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)]

模态类

帮助中的任何错误都受到高度赞赏, 谢谢, 维克斯

I am trying to implement a simple join operation with my two tables ,using hibernate 3 annotaions with spring3 mvc.

I have two tables:

Employee

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

Salary

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

I have created two annotated modal classes:

Employee.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;
    }

when i open a page i got the following error

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)]

Anything wrong on my modal class

Help is highly appreciated,
Thanks,
VKS

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月依秋水 2024-12-18 23:00:52

您的Set 工资是一种原始类型,hibernate 不知道如何将其“映射”到一个实体。
尝试将 targetEntity 添加到您的 @oneToMany 或使用 Set

Your Set of salaries is a raw type and hibernate doesn't know how to "map" it with one entity.
Try adding targetEntity to your @oneToMany or use Set<Salary>

嘿看小鸭子会跑 2024-12-18 23:00:52

我不是专家,但是我们不需要告诉hibernate SET 的类型应该是什么。因为在这种情况下,在我看来,我们告诉 hibernate SET 代表一对多关系,但是与哪个表?

在 *.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>

I am not an expert, But dnt we need to tell hibernate that what should be the type of SET. because in this case, it seems to me that we are telling hibernate SET represents one to many relationship, but with which table ??

In *.hbm.xml mapping files, I guess "table" attribute was used for this purpose.

e.g.

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文