EJB Note 4-Persistent Object Manager

Source: Internet
Author: User

 

Configuration File persistence. xml

<Persistence>
<Persistence-unit name = "example">
<JTA-data-source> JAVA:/defaultmysqlds </JTA-data-source>
<Properties>
<Property name = "hibernate. hbm2ddl. Auto" value = "Create-drop"/>
</Properties>
</Persistence-unit>
</Persistence>
 

Entitymanager object can be injected through dependency

If multiple persistence contents are configured in the persisitence. xml file, you must specify the persistence name when injecting the entitymanager object. You can use @ persistencecontext (unitname = "persistence name ")

1. persisitence. XML has only one persistent content.

@ Persistencecontext
Protected entitymanager em;

2. persisitence. XML has two or more persistent contents.

@ Persistencecontext (unitname = "Example "))
Protected entitymanager em;

 

1,

1.1 if you know the unique identifier of an object, you can use the find or getreference method to obtain the object.

@ Persistencecontext (unitname = "Example "))
Protected entitymanager em; person = em. find (person. class, 1); try {person P = em. getreference (person. class, 1);} catch (entitynotfoundexception e) {} the difference between the two methods is that when the object cannot be found, the find method returns NULL, while the getreference method throws an exception.

1.2 In addition to the above method, you can also create a query object through the createquery or createnamequery method, and then obtain the object through query. getreslutlist ().

1.3 createnativequery (); the input parameter of this method must be an SQL statement, which is different from the createquery () method.

2. Method for saving objects to the database persist ()

3. Update an object

3.1 when the object is being managed by the container, you can use the find method to obtain the object, and then use the set method to set the change value. To update data to a database in real time

You can call the em. Flush () method.

3.2 merge the merge method. This method is used when the entity is separated from the entitymanager management. Similarly, the flush () method can be used to achieve real-time synchronization with the database.

4. Delete. Delete em. Remove (person) from the database );

5. Refresh the entity refresh () to update the data in the database to the entity in the Manager (to the database to the manager. Database updates. When you refresh an object to a database, flush () (Manager to database) is used to update the object in the database.

6. Check whether the object is under management contans ().

7. Change the refresh mode em. setflushmode (); The default value is flushmodetype. auto occurs before the query statement is executed (except for find and getreference) or when the transaction is committed. The condition is that a large number of updates are used without a query. Flushmodetype. Commit refresh occurs only when the transaction is committed, and queries exist in a large number of conditional updates.

7. Actively detach all managed entities clear (). It can reduce memory consumption.

8. Refresh the object to the database. Flush ()

 

Command Parameter Query

Format: ": + parameter name"

@ Persistencecontext
Protectedentitymanager em;
...
Privatestringnamequery ()...{
// Personid
Queryquery = em. createquery ("selectpfrompersonpwherep. personid =: ID ");
Query. setparameter ("ID", newinteger (1 ));
Listresult = query. getresultlist ();
Stringbufferout = newstringbuffer ("****************** namequery
* *************** <Br> ");
If (result! = NULL )...{
Iteratoriterator = result. iterator ();
While (iterator. hasnext ())...{
Personperson = (person) iterator. Next ();
Out. append (person. getname () + "<br> ");
}
}
Return out. tostring ();
}

Location Parameter Query

The format is "? + Location Number"

Queryquery = em. createquery ("selectpfrompersonpwherep. personid =? 1 ");

Query. setparameter (1, newinteger (1 ));

Date Parameter

If the passed parameter is of the date or calendar type, you must use a special setparameter.

 

Public interface Query
...{
// Java. util. Date
Query. setparameter (stringname, java. util. datevalue, temporaltypetemporaltype );
// Java. util. Calendar
Query. setparameter (stringname, calendarvalue, temporaltypetemporaltype );
// Java. util. Date
Query. setparameter (intposition, datevalue, temporaltypetemporaltype );
// Java. util. Calendar
Query. setparameter (intposition, calendarvalue, temporaltypetemporaltype );
}

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.