Quantcast
Channel: Spring Community Forums - Data
Viewing all articles
Browse latest Browse all 297

How to enter data in two tables at one time in spring mvc hibernate

$
0
0
Article class:

@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;
}
ArticleDaoImpl Class:

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

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

}
ArticleServiceImpl class:

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

}
can you tell me that how to enter value in two tables at one time?? because i have to enter imei number in one table and imei and date and time in second table. i have perform working of second table but i didnt know how to enter values at one time in two tables. Please help me

Viewing all articles
Browse latest Browse all 297

Trending Articles