A Struts interview question

Source: Internet
Author: User

The question is: There are two tables with two fields for the news category table: nid (pk) sort there is a news content table with three fields cid (pk) nid (fk) title content requires you to select a news category using the drop-down list box and display the news titles of this category (displayed on the current page). I implemented it using Struts2 + Hibernate3.2 + JPA. database Script: create database if not exists news; drop table if exists newssort; create table newssort (nid int primary key AUTO_INCREMENT, sort varchar (50); drop table if exists news; create table news (cid int primary key AUTO_INCREMENT, title varchar (50) not null, content varchar (500) not null, nid int null); insert into newssort values (null, 'enter'); insert into newssort values (null, 'data'); insert into news values (null, 'good thing', 'good things are connected to Haha ', 1); insert into news values (null, 'bad thing ', 'bad thing', 1); insert into news values (null, 'Love is what ', 'Love is what, don't know yet', 2); insert into news values (null, 'who', 'test content', 2); select * from news; select * from newssort; two VO classes: News. java: package co M. vo; import java. io. serializable; import javax. persistence. entity; import javax. persistence. generatedValue; import javax. persistence. generationType; import javax. persistence. id; import javax. persistence. table; @ SuppressWarnings ("serial") @ Entity @ Table (name = "news") public class News implements Serializable {private Integer cid; private String title; private String content; @ Id @ GeneratedValue (strategy = G EnerationType. AUTO) public Integer getCid () {return cid;} public void setCid (Integer cid) {this. cid = cid;} public String getTitle () {return title;} public void setTitle (String title) {this. title = title;} public String getContent () {return content;} public void setContent (String content) {this. content = content ;}} Newssort. java: package com. vo; import java. io. serializable; import java. u Til. arrayList; import java. util. list; import javax. persistence. entity; import javax. persistence. generatedValue; import javax. persistence. generationType; import javax. persistence. id; import javax. persistence. joinColumn; import javax. persistence. onetovel; import javax. persistence. table; import org. hibernate. annotations. lazyCollection; import org. hibernate. annotations. lazyCollectionOption; @ SuppressWarnings ("Serial") @ Entity @ Table (name = "newssort") public class Newssort implements Serializable {private Integer nid; private String sort; private List <News> news = new ArrayList <News> (); @ onetoworkflow @ JoinColumn (name = "nid") @ LazyCollection (LazyCollectionOption. FALSE) public List <News> getNews () {return news;} public void setNews (List <News> news) {this. news = news;} @ Id @ GeneratedValue (strategy = Genera TionType. AUTO) public Integer getNid () {return nid;} public void setNid (Integer nid) {this. nid = nid;} public String getSort () {return sort;} public void setSort (String sort) {this. sort = sort;} write a test class to test a persistent layer operation: package com. test; import java. util. iterator; import org. hibernate. session; import org. hibernate. cfg. annotationConfiguration; import org. junit. after; import org. junit. before; import com. Vo. news; import com. vo. newssort; public class Test {private Session session; @ Before public void setUp () {session = new AnnotationConfiguration (). configure (). buildSessionFactory (). openSession () ;}@ After public void tearDown () {session. close () ;}@ SuppressWarnings ("unchecked") @ org. junit. test public void testFind () {@ SuppressWarnings ("unused") // List <Newssort> newssort = session. createCriteria (Newssort. class ). list (); Newssort newssort = (Newssort) session. load (Newssort. class, 2); for (Iterator <News> I = newssort. getNews (). iterator (); I. hasNext ();) {String title = I. next (). getTitle (); System. out. println (title) ;}}write ActionNewsAction: package com. web. action; import java. util. list; import java. util. map; import org. hibernate. session; import org. hibernate. cfg. annotationConfiguration; import Com. opensymphony. xwork2.ActionContext; import com. opensymphony. xwork2.ActionSupport; import com. vo. news; import com. vo. newssort; @ SuppressWarnings ({"serial", "unchecked"}) public class NewsAction extends ActionSupport {private Session session; private Integer sortid; public Integer getSortid () {return sortid ;} public void setSortid (Integer sortid) {this. sortid = sortid;} public void init () {se Ssion = new AnnotationConfiguration (). configure (). buildSessionFactory (). openSession ();} public String findNewssort () {this. init (); List <Newssort> sorts = session. createCriteria (Newssort. class ). list (); Map request = (Map) ActionContext. getContext (). get ("request"); request. put ("sorts", sorts); session. close (); return SUCCESS;} public String findNews () {this. init (); System. out. println ("findNews" ); List <Newssort> sorts = session. createCriteria (Newssort. class ). list (); Newssort newssort = (Newssort) session. load (Newssort. class, sortid); List <News> news = newssort. getNews (); Map request = (Map) ActionContext. getContext (). get ("request"); request. put ("sorts", sorts); request. put ("news", news); session. close (); return SUCCESS ;}} hibernate. cfg. xml: <? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE hibernate-configuration PUBLIC "-// Hibernate/Hibernate Configuration DTD 3.0 // EN "" http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd "> <Hibernate-configuration> <session-factory> <property name =" dialect "> org. hibernate. dialect. mySQLDialect </property> <property name = "connection. driver_class "> com. mysql. jdbc. driver </property> <property name = "connection. url "> jdbc: mysql: // localhost: 3306/news </property> <property name =" connection. username "> root </property> <property name =" connection. password "> root </property> <property name =" show_ SQL "> tr Ue </property> <! -- Object class ing --> <mapping class = "com. vo. news "/> <mapping class =" com. vo. newssort "/> </session-factory>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.