我是否需要在 java 端进行额外的编码以实现 hibernate 一对多注释

发布于 2024-10-20 10:58:09 字数 3657 浏览 1 评论 0原文

这是我的 hibernate 类

package com.vaannila.domain;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.springframework.web.bind.annotation.ModelAttribute;


@Entity
@Table(name = "countries")
public class Country {

@Id
@Column(name = "country_id")
@GeneratedValue
private  Integer country_id;


@Column(name = "country_name")
private String country_name;


public Country(Integer country_id , String name ){

    this.country_name = name;
    this.country_id = country_id;

}


/**
 * @param country_id the country_id to set
 */
public void setCountry_id(Integer country_id) {
    this.country_id = country_id;
}
/**
 * @return the country_id
 */
public Integer getCountry_id() {
    return country_id;
}
/**
 * @param country_name the country_name to set
 */
public void setCountry_name(String country_name) {
    this.country_name = country_name;
}
/**
 * @return the country_name
 */
public String getCountry_name() {
    return country_name;
}



}

Person java

package com.vaannila.domain;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.CascadeType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;

import javax.validation.constraints.*;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;




/**
 * A simple POJO representing a Person
 */
@Entity
@Table(name = "PERSON")
public class Person implements Serializable {

 private static final long serialVersionUID = -5527566248002296042L;


 @Id
 @Column(name = "ID")
 @GeneratedValue
 private Integer id;


 @Column(name = "FIRST_NAME")
 private String firstName;

 @Column(name = "LAST_NAME")
 private String lastName;

 @Column(name = "MONEY")
 private Double money;

 @OneToMany(cascade = CascadeType.ALL)
 @JoinTable(name = "person_countries", joinColumns = { @JoinColumn(name = "person_id") }, 
         inverseJoinColumns = { @JoinColumn(name = "country_id") })
 private List<Country> student_countries ; 


 public List<Country> getStudent_countries() {
  return this.student_countries;
    }



 public Integer getId() {
  return id;
 }

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

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public Double getMoney() {
  return money;
 }

 public void setMoney(Double money) {
  this.money = money;
 }
}

jsp 形式

<tr><td>Country :</td>
            <td><form:checkboxes path="student_countries" items="${countryList}" itemValue="country_id" itemLabel="country_name" /></td>
    </tr>
    </table>

DAO 逻辑

 public void add(Person person) {
      logger.debug("Adding new person");

      // Retrieve session from Hibernate
      Session session = sessionFactory.getCurrentSession();

      // Save
      session.save(person);
     }

但我的国家没有添加到数据库中,所有其他内容都在个人表中,但不在关系表中。

Here are my hibernate classes

package com.vaannila.domain;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.springframework.web.bind.annotation.ModelAttribute;


@Entity
@Table(name = "countries")
public class Country {

@Id
@Column(name = "country_id")
@GeneratedValue
private  Integer country_id;


@Column(name = "country_name")
private String country_name;


public Country(Integer country_id , String name ){

    this.country_name = name;
    this.country_id = country_id;

}


/**
 * @param country_id the country_id to set
 */
public void setCountry_id(Integer country_id) {
    this.country_id = country_id;
}
/**
 * @return the country_id
 */
public Integer getCountry_id() {
    return country_id;
}
/**
 * @param country_name the country_name to set
 */
public void setCountry_name(String country_name) {
    this.country_name = country_name;
}
/**
 * @return the country_name
 */
public String getCountry_name() {
    return country_name;
}



}

Person java

package com.vaannila.domain;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.CascadeType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;

import javax.validation.constraints.*;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;




/**
 * A simple POJO representing a Person
 */
@Entity
@Table(name = "PERSON")
public class Person implements Serializable {

 private static final long serialVersionUID = -5527566248002296042L;


 @Id
 @Column(name = "ID")
 @GeneratedValue
 private Integer id;


 @Column(name = "FIRST_NAME")
 private String firstName;

 @Column(name = "LAST_NAME")
 private String lastName;

 @Column(name = "MONEY")
 private Double money;

 @OneToMany(cascade = CascadeType.ALL)
 @JoinTable(name = "person_countries", joinColumns = { @JoinColumn(name = "person_id") }, 
         inverseJoinColumns = { @JoinColumn(name = "country_id") })
 private List<Country> student_countries ; 


 public List<Country> getStudent_countries() {
  return this.student_countries;
    }



 public Integer getId() {
  return id;
 }

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

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public Double getMoney() {
  return money;
 }

 public void setMoney(Double money) {
  this.money = money;
 }
}

jsp form

<tr><td>Country :</td>
            <td><form:checkboxes path="student_countries" items="${countryList}" itemValue="country_id" itemLabel="country_name" /></td>
    </tr>
    </table>

DAO Logic

 public void add(Person person) {
      logger.debug("Adding new person");

      // Retrieve session from Hibernate
      Session session = sessionFactory.getCurrentSession();

      // Save
      session.save(person);
     }

But my countries are not added in the database, all other things go in person table but not in relationship table.

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

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

发布评论

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

评论(2

怀中猫帐中妖 2024-10-27 10:58:09

测试了你的代码,对我有用。确保您的配置设置正确且未被覆盖。

Tested your code, works for me. Make sure your config settings are correct and not overridden.

离笑几人歌 2024-10-27 10:58:09

我猜问题是你没有 1:n 关系,你有一个:m!因为你一个人有很多个学生国家,而且我猜每个国家都可以有多个人。

因此,将 Person 中的 @OneToMany(cascade = CascadeType.ALL) 替换为 @ManyToMany 关系。

I guess that the probem is that you do not have a 1:n Releationship, you have a n:m! Because your person have many studend contryies, and I guess that every country can have more than one person.

So replace the @OneToMany(cascade = CascadeType.ALL) in Person by an @ManyToMany relationship.

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