Object Bean (12) one-to-one ing

Source: Internet
Author: User

@ Onetoone: four attributes

1. targetentity

The targetentity attribute is a class attribute. Defines the types of object classes that are subordinate to an object in a one-to-one relationship.

2. mappedby

The mappedby attribute is a string type attribute. The value of the mappedby attribute is the attribute name of the current object in the associated object. edby can be used to define the bidirectional relationship between object classes. If there is a unidirectional relationship between classes, no definition is required. If there is a bidirectional relationship between classes, we need to use this attribute for definition. Otherwise, Data Consistency may occur.

3. Cascade

The cascade attribute type is cascadetype. The cascade attribute defines the cascade relationship between objects. The cascade relation defined using the cascade attribute is considered by the container to take the same action on the current class object and its associated class object, and this relation is called recursively.

The cascade value can only be from cascadetype. persist and cascadetype. remove (cascade delete), cascadetype. refresh (cascade refresh), cascadetype. select one or more in Merge (cascade update. Another more convenient choice is to use cascadetype. All, which means to select all the above four items.

4. Fetch

The fetch attribute is a fetchtype attribute. Optional items include: fetchtype. eager and fetchtype. lazy. the former indicates that the slave class of the association is loaded when the main class is loaded, and the latter indicates that the slave class of the association is loaded only when it is accessed by itself. The default value is fetchtype. Eager.

5. Optional

The optional attribute is a Boolean attribute. The optional attribute is used to define whether the slave Class Object of the association relationship must exist. If it is set to false, this attribute cannot be set to null. The default value is true. If the optional attribute of the annotation is set to true, the object may not exist.

 

 Package Com. Persia. JPA; Import Java. Io. serializable; Import Javax. Persistence. cascadetype; Import Javax. Persistence. column; Import Javax. Persistence. entity; Import Javax. Persistence. generatedvalue;Import Javax. Persistence. generationtype; Import Javax. Persistence. ID; Import Javax. Persistence. onetoone; Import Javax. Persistence. Table; @ entity @ table (name =" Person ") Public   Class Person Implements Serializable { Private Integer ID; Private String name; Private Short age;Private Address; @ ID @ generatedvalue (Strategy = generationtype. Auto) 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 ;} Public Short getage (){ Return Age ;} Public   Void Setage (short age ){ This . Age = age ;}@ onetoone (optional = True , Cascade = cascadetype. All, mappedby =" Person ") Public Address getaddress (){ Return Address ;} Public   Void Setaddress (Address ){ This . Address = address ;}}
 
Bytes -------------------------------------------------------------------------------------------------------------------------

1. The javax. Persistence. joincolumn annotation can be used with the javax. Persistence. onetoone annotation to define the tables of the primary class in the association relationship in the database.

This annotation is optional when fields are associated with the primary key of the slave class in the association relationship. If this annotation is not provided, openjpa uses the "Object name_id" and the primary key of the associated table by default.

Fields.

2. joincolumn annotation supports two important attributes: Name and referencedcolumnname.

(1) Name

The type of the name attribute is string. The name attribute is used to specify the name of the field associated with the primary key of the class in the table corresponding to the primary class in the association.

(2) referencedcolumnname

The type of the referencedcolumnname attribute is string. The referencedcolumnname attribute specifies the table in the Association relation that corresponds to the primary class in the Association relation.

The name of the fields that form an association relationship. It is usually used when the association fields of the slave class in the association relationship are not their own primary keys.

Package Com. Persia. JPA; Import Java. Io. serializable; Import Javax. Persistence. cascadetype; Import Javax. Persistence. entity; Import Javax. Persistence. generatedvalue; Import Javax. Persistence. generationtype; Import Javax. Persistence. ID; Import Javax. Persistence. joincolumn; Import Javax. Persistence. onetoone; Import Javax. Persistence. Table; @ entity @ table (name ="Address ") Public   Class Address Implements Serializable { Private Integer ID; Private String city; Private String Street; Private Person; @ ID @ generatedvalue (Strategy = generationtype. Auto) Public Integer GETID (){ Return ID ;} Public   Void Setid (integer ID ){This . ID = ID ;} Public String getcity (){ Return City ;} Public   Void Setcity (string city ){ This . City = city ;} Public String getstreet (){ Return Street ;} Public   Void Setstreet (string Street ){ This . Street = Street;} @ onetoone (optional = False , Cascade = cascadetype. All) @ joincolumn (name =" Person_id ", Referencedcolumnname =" ID ", Unique = True ) Public Person getperson (){ Return Person ;} Public   Void Setperson (person ){ This . Person = person ;}}

Use the joincolumn annotation to set the association fields between two objects corresponding to the database tables.

The name attribute specifies the name of the field involved in the association in the table corresponding to the primary class in the association relationship,

The referencedcolumnnam attribute specifies the name of the field involved in the association from the table corresponding to the class in the association relationship.

 

--------------------------------------------------------------------

Entity operations:

 Package Com. Persia. JPA; Import Javax. EJB. Remote; Import Javax. EJB. stateless; Import Javax. Persistence. entitymanager; Import Javax. Persistence. persistencecontext; @ stateless @ remote ({onetoonedao. Class }) Public   Class Onetoonedaobean Implements Onetoonedao {@ persistencecontextPrivate Entitymanager em; @ override Public   Void Deleteaddress (integer ID ){ // Todo auto-generated method stub Address a = em. Find (address. Class , ID); em. Remove (a) ;}@ override Public   Void Deleteperson (integer ID ){ // Todo auto-generated method stub Person P = em. Find (person. Class , ID); em. Remove (p) ;}@ override Public Person getpersonbyid (integer ID ){// Todo auto-generated method stub Person P = em. Find (person. Class , ID ); Return P ;}@ override Public   Void Insertperson (string name, Short Age, string city, string Street ){ // Todo auto-generated method stub Person P = New Person (); p. setname (name); p. setage (AGE); Address = New Address (); Address. setcity (city); Address. setstreet (street); Address. setperson (p); p. setaddress (Address); em. persist (p) ;}@ override Public   Void Updatepersoninfo (integer ID, string newname, string newstreet ){ // Todo auto-generated method stub Person P = em. Find (person. Class , ID); p. setname (newname); address a = P. getaddress (); A. setstreet (newstreet );}}

Q: Do I not need merge in updatepersoninfo?

If it is set by setter on the client, merge is required.

 

Certificate -------------------------------------------------------------------------------------------------------------------------------------

Client:

 

 Package Com. Persia. JPA. test; Import Java. util. properties; Import Javax. Naming. initialcontext; Import Javax. Naming. namingexception; Import Com. Persia. JPA. OTO. onetoonedao; Import Com. Persia. JPA. OTO. person; Public   Class Test { /*** @ Param ARGs * @ throws namingexception */  Public   Static  Void Main (string [] ARGs) Throws Namingexception {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 context = New Initialcontext (props ); Try {Onetoonedao Dao = (onetoonedao) Context. Lookup (" Onetoonedaobean/remote "); // Dao. insertperson ("Persia", new short (short) 26), "Beijing", "Shangdi ");  // Dao. insertperson ("Linda", new short (short) 26), "Beijing", "Shangdi ");  // Dao. deleteperson (New INTEGER (8 )); Person P = Dao. getpersonbyid ( New INTEGER (5); system. Out. println (P. getname () +" - "+ P. getaddress (). getstreet (); Dao. updatepersoninfo ( New INTEGER (5 )," Persiacai ","Fuji "); Dao. deleteaddress ( New INTEGER (10 ));} Catch (Exception e) {e. printstacktrace ();}}}

Because the person and address are two-way and cascade, deleting any one will delete the other.

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.