Entity Bean (8) entitymanager

Source: Internet
Author: User

 Package Com. Persia. JPA. onetable; Import Java. Io. serializable; Import Javax. Persistence. column; Import Javax. Persistence. entity; Import Javax. Persistence. generatedvalue; Import Javax. Persistence. ID; Import Javax. Persistence. Table; @ entity @ table (name =" Person ") Public   Class Person Implements Serializable { Private Integer ID; Private String name; Private Short age; @ ID @ generatedvalue Public Integer GETID (){ Return ID ;} Public   Void Setid (integer ID ){ This . ID = ID;} @ column (name =" Personname ", Nullable = False , Length = 32) Public String getname (){Return Name ;} Public   Void Setname (string name ){ This . Name = Name ;}@ column (nullable = False ) Public Short getage (){ Return Age ;} Public   Void Setage (short age ){ This . Age = age ;}}
 Package Com. Persia. entity. Dao; Import Java. util. List;Import Javax. EJB. Remote; Import Javax. EJB. stateless; Import Javax. Persistence. entitymanager; Import Javax. Persistence. persistencecontext; Import Javax. Persistence. query; Import Com. Persia. JPA. onetable. person; @ stateless @ remote ({persondao. Class }) Public   Class Persondaobean Implements Persondao {@ persistencecontext Protected Entitymanager em; @ override Public Person getpersonbyid ( Int Personid ){ Return Em. Find (person. Class , Integer. valueof (personid);} @ override Public List getpersonlist ( Int Max, Int Whichpage ){ // Todo auto-generated method stub  Try { Int Index = (whichpage-1) * max; query = em. createquery ("From person P order by ID ASC "); List list = query. setmaxresults (max). setfirstresult (INDEX). getresultlist (); em. Clear (); Return List ;} Catch (Exception e) {e. printstacktrace (); Return   Null ; }}@ Override Public String getpersonnamebyid ( Int Personid) {person P = em. Find (person. Class , Integer. valueof (personid )); Return P. getname () ;}@ override Public  Boolean Insertperson (string name, Short Age ){ Try {Person P = New Person (); p. setname (name); p. setage (short. valueof (AGE); em. persist (p );} Catch (Exception e) {e. printstacktrace (); Return   False ;} Return   True ;} @ Override Public   Boolean Updateperson (person ){// Todo auto-generated method stub  Try {Em. Merge (person );} Catch (Exception e) {e. printstacktrace (); Return   False ;} Return   True ;}}

 

JSP test page

 

<% Properties props = New Properties (); props. setproperty (" Java. Naming. Factory. Initial "," Org. jnp. Interfaces. namingcontextfactory "); Props. setproperty ("Java. Naming. provider. url "," Localhost: 1099 "); Props. setproperty (" Java. Naming. Factory. url. pkgs "," Org. JBoss. Naming "); Initialcontext CTX = New Initialcontext (props ); Try {Persondao = (persondao) CTX. Lookup (" Persondaobean/remote "); Persondao. insertperson (" Persia ",( Short ) 26); Out. println (persondao. getpersonnamebyid (1); person = persondao. getpersonbyid (1); person. setname (" Linda "); Persondao. updateperson (person); Out. println (" <Br> =============== pagination display ===========================< br/> "); String Index = request. getparameter (" Index "); If (Index = Null |"  ". Equals (index. Trim () Index =" 1 "; Int Max = 2; Int Whichpage = integer. parseint (INDEX); List list = persondao. getpersonlist (max, whichpage ); If (List! = Null ) {Iterator it = List. iterator (); While (It. hasnext () {person P = (person) it. Next (); Out. println (" No: "+ P. GETID () +" Name: "+ P. getname () +" <Br> ");}}} Catch (Exception e) {out. println (E. getmessage () ;}%>

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.