簡體   English   中英

我是否需要在Java端進行額外的編碼以休眠一對多注釋

[英]do i need extra coding in java side for hibernate one-to-many annotations

這是我的休眠課程

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



}

人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);
     }

但是我的國家/地區沒有添加到數據庫中,所有其他內容都放在個人表中,而不在關系表中。

我猜測,probem是,你沒有一個1:N Releationship,您有:M! 因為您的人有很多志趣相投的職位,我想每個國家都可以有一個以上的人。

因此,將Person中的@OneToMany(cascade = CascadeType.ALL)替換為@ManyToMany關系。

測試了您的代碼,對我有用。 確保您的配置設置正確無誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM