如何使用具有关系图的表单使用hibernate将值插入多个表?

时间:2017-08-22 09:18:52

标签: java spring hibernate

我有一份注册表格:

<form action="${ pageContext.request.contextPath }/customer/add" method="post">
<div>
    <label>Name: </label>
    <input type="text" name="name">
</div>
<div>
    <label>Address: </label>
    <input type="text" name="address.address">
</div>
<input type="submit" value="Insert">

这里我想将此表单的值插入到具有以下pojo的客户和地址帐户中:

Cusotmer.java

@Id
@GeneratedValue
@Column
private Long id;
@Column
private String name;
@OneToOne
@JoinColumn(name = "address_id")
private Address address;

Address.java

@Id
@GeneratedValue
@Column
private Long id;
@Column
private String address;

我创建了一个daoImpl方法来添加客户及其各自的地址:

CustomerDaoImpl

@Override
@Transactional
public void addCustomer(Customer c) {
    session = sessionFactory.openSession();
    session.beginTransaction();
    session.save(c);
    session.getTransaction().commit();
    session.close();
}

AddressDaoImpl

@Override
@Transactional
public void addAddress(Address a) {
    session = sessionFactory.openSession();
    session.beginTransaction();
    session.save(a);
    session.getTransaction().commit();
    session.close();        
}

CustomerController

@Autowired
private CustomerDao customerDao;

@Autowired
private AddressDao addressDao;

@RequestMapping(value="/customer/add", method = RequestMethod.POST)
public String postCustomer(@ModelAttribute Customer c, @ModelAttribute Address a){
    addressDao.addAddress(a);
    c.setAddress(a);
    customerDao.addCustomer(c);
    return "redirect:/customer";
}

插入操作仅适用于地址值。客户与相应的address_id一起添加,但在地址表本身中,仅在创建id时未插入地址名称。

3 个答案:

答案 0 :(得分:1)

添加级联选项,以便在保存客户时,相应的地址已保存。

@OneToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "address_id")
private Address address;

简化控制器:

@RequestMapping(value="/customer/add", method = RequestMethod.POST)
public String postCustomer(@ModelAttribute Customer c){
    customerDao.addCustomer(c);
    return "redirect:/customer";

}

答案 1 :(得分:0)

我不确定我是否可以帮助您解决问题,但可以尝试纠正错误。

1)@Transactional注释用于服务层,而不是DAO。并且您实际上不使用@Transactional注释和session.beginTransaction();,请选择其中一个

2)您不必每次都放置@Column注释,在您必须指定其中的某些属性之前它是无用的。

@Column
private String name;

3)使用弹簧控制器,您可以使用@PostMapping。有关此处的更多信息:https://spring.io/guides/gs/handling-form-submission/

希望这可以帮助,不知何故。

答案 2 :(得分:0)

更改Address pojo中私有可变地址的名称,使其与Customer pojo中的地址对象不对。

...............
    // create new views (invoked by the layout manager)
    @Override
    public NewsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.news_row_layout,parent,false);
        return new NewsHolder(view);
    }

    @Override
    public void onBindViewHolder(NewsHolder holder, int position) {

        final NewsModel currentNews = mNews.get(position);
        Picasso.with(holder.itemView.getContext());
        Picasso.with(holder.itemView.getContext()).load(currentNews.getTeaserImageSmall().getSrc()).into( holder.newsImage );

        holder.newsImage.setImageResource( R.drawable.image1 );
        holder.newsHeadline.setText(currentNews.getTitle());
        holder.newsDate.setText(currentNews.getPostDate());

    }

    @Override
    public int getItemCount() {
        return mNews.size();
    }


    public class NewsHolder extends RecyclerView.ViewHolder {

        public CardView cardView;
        public ImageView newsImage;
        public TextView newsHeadline;
        public TextView newsDate;

        public NewsHolder(View itemView) {
            super(itemView);
            newsImage=(ImageView)itemView.findViewById(R.id.news_picture);
            newsHeadline=(TextView)itemView.findViewById(R.id.news_headline);
            newsDate=(TextView) itemView.findViewById(R.id.news_date);
            cardView=(CardView)itemView.findViewById(R.id.cardview_news);
        }
    }
}

模拟将表单地址输入名称从 address.address 更改为 aName

@Id
@GeneratedValue
@Column
private Long id;
@Column
private String aName;    // change is here...

控制器中的休息是一样的。

<form action="${ pageContext.request.contextPath }/customer/add" method="post">
<div>
    <label>Name: </label>
    <input type="text" name="name">
</div>
<div>
    <label>Address: </label>
    <input type="text" name="aName">    // see the change here...
</div>
<input type="submit" value="Insert">