Hibernate一对多新插入新的没有链接表

时间:2014-12-20 14:45:52

标签: hibernate annotations

Customer.java

@Entity
@Table(name="tb_customer")
public class Customer {
    public Customer() { }

    @Id
    @Column(name="customer_id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int customerId;
    @Column(name="store_name")
    private String storeName;

    @SuppressWarnings("unchecked")
    @ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(name="tb_customer_pic", 
                joinColumns={@JoinColumn(name="customer_id", referencedColumnName="customer_id")},
                inverseJoinColumns={@JoinColumn(name="person_id", referencedColumnName="person_id")})
    private List<Person> picList = LazyList.decorate(new ArrayList<Person>(), FactoryUtils.instantiateFactory(Person.class));

    //getter setter truncated
}

Person.java

@Entity
@Table(name="tb_person")
public class Person {
    public Person() { }

@Id 
@Column(name="person_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int personId;
@Column(name="first_name")
private String firstName;

@SuppressWarnings("unchecked")
@OneToMany(mappedBy="personEnt", cascade=CascadeType.ALL)
private List<PhoneList> phoneList = LazyList.decorate(new ArrayList<PhoneList>(), FactoryUtils.instantiateFactory(PhoneList.class));

//getter setter truncated
}

PhoneList.java

@Entity
@Table(name="tb_phonelist")

public class PhoneList {
    public PhoneList() { }

    @Id
    @Column(name="phonelist_id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)   
    private int phoneListId;
    @Column(name="provider")
    private String providerName;

    @ManyToOne
    @JoinColumn(name="person_id")
    private Person personEnt;

    //getter setter truncated
}

CustomerController.java
在这里我得到了json

    @RequestMapping(value="/save", method = RequestMethod.POST)
    public String saveCustomer(Locale locale, Model model, @ModelAttribute("customerForm") Customer cust) {
        logger.info("[saveCustomer] " + cust.toString());

        if (cust.getCustomerId() == 0) { customerManager.addCustomer(cust); }
        else { customerManager.editCustomer(cust); }

        model.addAttribute(Constants.PAGEMODE, Constants.PAGEMODE_LIST);
        model.addAttribute(Constants.ERRORFLAG, Constants.ERRORFLAG_HIDE);

        return "redirect:/customer";
    }

jason从cust.toString()

生成
Customer [customerId=0, storeName=aa, bankAccList=[], picList=[Person [personId=0, firstName=a, lastName=a, addressLine1=a, addressLine2=a, addressLine3=a, emailAddr=a, photoPath=, phoneList=[PhoneList [phoneListId=0, providerName=b, phoneNumber=b, phoneStatus=b, phoneNumRemarks=b], PhoneList [phoneListId=0, providerName=b, phoneNumber=b, phoneStatus=b, phoneNumRemarks=b]]]]]

CustomerDAOImpl.java

public void addCustomer(Customer cust) {
logger.info("[addCustomer] " + "");

Session session = this.sessionFactory.getCurrentSession();
session.persist(cust);

logger.info("Customer added successfully, Customer Details = " + cust.toString());      

}

表格配置

tb_customer
tb_person
tb_customer_pic(用于链接)

tb_person
tb_phonelist(没有链接表,tb_phonelist列包含person_id用于链接)

客户实体有许多人为PIC,每个人都有许多phonelist / number。到目前为止,我能够保存(插入)直到所有表,但对于tb_phonelist,person_id列在保存时总是为空,我在这里做错了什么? 感谢

1 个答案:

答案 0 :(得分:0)

这可能是因为手机实例没有设置人物对象。从json构造Customer实例后,您应该遍历来自person的phoneList并将person对象设置为每个Phone对象。

    @RequestMapping(value="/save", method = RequestMethod.POST)
    public String saveCustomer(Locale locale, Model model, @ModelAttribute("customerForm") Customer cust) {

        for(Person p : cust.getPicList()) {
          for(PhoneList phone : p.getPhoneList()) { 
             phone.setPersonEnt(p);
          }
        }

        logger.info("[saveCustomer] " + cust.toString());

        if (cust.getCustomerId() == 0) { customerManager.addCustomer(cust); }
        else { customerManager.editCustomer(cust); }

        model.addAttribute(Constants.PAGEMODE, Constants.PAGEMODE_LIST);
        model.addAttribute(Constants.ERRORFLAG, Constants.ERRORFLAG_HIDE);

        return "redirect:/customer";
    }

让我知道它是否有效。