Hibernate 休眠一对多新插入不带链接的新表

Hibernate 休眠一对多新插入不带链接的新表,hibernate,annotations,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

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
}
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";
    }
从cust.toString生成的json

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_客户 结核病患者 tb_客户_链接图片

结核病患者 tb_phonelist没有链接表,tb_phonelist列包含用于链接的人员id

客户实体有许多人作为PIC,每个人都有许多电话列表/号码。到目前为止,我可以保存INSERT直到所有表,但对于tb_phonelist,保存时person_id列始终为空,我在这里做错了什么?
谢谢

这可能是因为电话实例没有设置person对象。从json构造客户实例后,应该循环person的电话列表,并将person对象设置到每个电话对象中

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

让我知道它是否有效。

粘贴创建cust实例的代码,即调用AddCustomerHanks的方法实现,以获得快速回复这是customer的json: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,PhoneNumRmarks=b],phoneList[phoneListId=0,providerName=b,phoneNumber=b,phoneStatus=b,PhoneNumRmarks=b]]]因此,对于上面的json字符串,您只需使用gson将其转换为java对象?将完整的方法粘贴到您的question.question updated中。谢谢,但person_id实际上是自动生成的,请参见person.java。这是否意味着我无法在1 go中插入,只需调用persistcustIt即可。是否已在phone对象中设置person实例?对不起,b但是我不明白,你能提供我需要做的更改吗?Tqi Andy,非常感谢它工作得很好。但是经过考虑,我将它更改为使用bridge table并使用@ManyToMany annotation.sry stackoverflow的新功能:
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());      
    @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";
    }