如何从表单中获取数据并将其存储在spring mvc hibernate中的多个表中

时间:2013-05-19 17:10:35

标签: java spring hibernate model-view-controller

我有这些课程:

文章类:

@Entity
@Table(name = "imei")
public class Article {
    @Id
    //@GeneratedValue
    @Column(name = "imei1",nullable = false)
    private Long imeiNo;
    @Column(name = "date_added")
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
        private Date addedDate;
    public Article() {      
    }
        public Long getImeiNo() {
        return imeiNo;
    }
    public void setImeiNo(Long imeiNo) {
        this.imeiNo = imeiNo;
    }
        public Date getAddedDate() {
        return addedDate;
    }

    public void setAddedDate(Date addedDate) {
        this.addedDate = addedDate;
    }   

ArticleDao课程:

public interface ArticleDao {
// To Save the article detail
public void saveArticle(Article article );

// To get list of all articles
public List<Article> listArticles();
}

ArticleDaoImpl类:

@Repository("articleDao")
public class ArticleDaoImpl implements ArticleDao {

    @Autowired
    private SessionFactory sessionFactory;

    // To Save the article detail
    public void saveArticle(Article article) {
        article.setAddedDate(new Date());

        sessionFactory.getCurrentSession().saveOrUpdate(article);
    }

    // To get list of all articles
    @SuppressWarnings("unchecked")
    public List<Article> listArticles() {       
        return (List<Article>) sessionFactory.getCurrentSession().createCriteria(Article.class).list();
    }
}

ArticleController:

@Controller
@RequestMapping("/articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ModelAndView saveArticle(@ModelAttribute(" article") Article  article,
            BindingResult result) {
         articleService.addArticle( article);
        return new ModelAndView("redirect:/articles.html");
    }

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView listArticles() {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("articles",  articleService.listArticles());

        return new ModelAndView("articlesList", model);
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView addArticle(@ModelAttribute("article") Article article,
            BindingResult result) {
        return new ModelAndView("addArticle");
    }

}

ArticleService:

public interface ArticleService {

public void addArticle(Article article);

public List<Article> listArticles();
}

ArticleServiceImpl类:

@Service("articleService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleDao articleDao;

    public ArticleServiceImpl() {
    }

    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public void addArticle(Article article) {
        articleDao.saveArticle(article);
    }

    public List<Article> listArticles() {
        return articleDao.listArticles();
    }

}

现在创建了另一个类, OnlyImei.java

@Entity
@Table(name = "onlyimei")
public class OnlyImei {
@Id
    @Column(name = "imei1",nullable = false)
private Long imeiNo;
    public OnlyImei() {     
}
public Long getImeiNo() {
    return imeiNo;
}
      public void setImeiNo(Long imeiNo) {
    this.imeiNo = imeiNo;
}
  }

您能告诉我如何一次在两张桌子中输入值吗?我必须在一张表中输入imei号码,在第二张表中输入imei和日期和时间。我已经制作了第二个表,但我不知道如何在两个表中一次输入值。

2 个答案:

答案 0 :(得分:1)

使用您的服务方式。写入事务中的两个表。我没有看到第二个DAO类,所以我为这个例子做了一个。

@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void addArticleToBook(Article article, Book book) {
    articleDao.saveArticle(article);
    bookDao.addArticle(article, book.getId());
}

工作将在同一笔交易中完成。

实际上,上述情况可能不是最好的。鉴于Hibernate支持关系的方式,如果你正确配置了Hibernate并使用了Spring Hibernate模板,你应该能够做到这一点:

@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void addArticleToBook(Article article, Long bookId) {
    this.sessionFactory.getCurrentSession()
                       .loadBookById(bookId)
                       .getArticles()
                       .add(article);
}

答案 1 :(得分:0)

您需要在服务中创建一个方法,(您可以扩展addArticle()方法)。

你注入两个Daos(如果有两个),一个是Table1Dao,另一个是Table2Dao。然后在这个服务方法中,你做:

table1Dao.addxxx(article)
table2Dao.addxxx(article)

参数是灵活的(文章,整数,数组,列表,其他类型),这取决于你是如何实现dao方法的。重要的是,服务方法应该在一个事务中执行。