0

我有这些课程:

文章类:

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

文章道类:

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

文章控制器:

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

}

文章服务:

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以及日期和时间。我已经制作了第二张表,但我不知道如何在两个表中一次输入值。

4

2 回答 2

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);
}
于 2013-05-19T17:20:13.333 回答
0

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

你注入两个道(如果有两个)一个是Table1Dao,另一个是Table2Dao。然后在此服务方法中,您可以:

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

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

于 2013-05-19T17:21:36.010 回答