Java persistence API (JPA) 5th-link Creation

Source: Internet
Author: User
Tags object object

Objective: To establish a relationship between entity classes to reflect the associations between entities in a database. Main Content: N create database table n add Test Data n generate object class n relational analysis n primary key class 1. instance description this instance simulates a simple Student Score Management System, to illustrate the one-to-one relationship, each student has a computer. The entities involved include students, computers, courses, and scores. For more information, see Create a database table. The relationship is as follows: n each computer belongs to one student; n students and courses are many-to-many; n students and scores are one-to-many; n there is a one-to-many relationship between the course and the score. 2. The statement for creating a database table is as follows: Create Table student (s_id char (10) Not null, s_name char (10) Not null, s_birthday date, primary Key (s_id); Create Table computer (c_id char (10) Not null, s_id char (10), c_price float, primary key (s_id), foreign key (s_id) references student (s_id); Create Table course (c_id char (8) Not null, c_name varchar (50) not null, primary key (c_id )); create Table score (s_id char (10) Not NUL L, c_id char (8) Not null, grade float, primary key (s_id, c_id), foreign key (s_id) References student (s_id), foreign key (c_id) references course (c_id); 3. The SQL statement for adding test data to add test data is as follows: insert into student values ('20140901', 'zhang Xiaofei ', '2017-03-04 '); insert into student values ('201312', 'liuhui', '2017-11-04'); insert into student values ('20170101', 'payer ', '2014-9-9 '); insert into student values ('20140901 ', 'Guo hao', '2014-6-5 '); insert into computer values ('cp001', '000000', 1987); insert into computer values ('cp002 ', '20140901', 0511370102); insert into computer values ('cp003 ', '000000', 8600); insert into computer values ('cp004', '20140901', 0511370103 ); insert into course values ('jk0301 ', 'java web developer'); insert into course values ('jk0302', 'java ee '); insert into course values ('jk0401 ', '.. Net '); Insert into course values ('jk0402 ', 'c # '); insert into score values ('20170301', 'jk0301 ', 70 ); insert into score values ('20170302', 'jk0302', 71); insert into score values ('20170302', 'jk0401', 72 ); insert into score values ('20170402 ', 'jk0402', 73); insert into score values ('20170301', 'jk0301 ', 90 ); insert into score values ('20170302', 'jk0302', 91); insert into score values ('20160302', 'jk0401', 9 2); insert into score values ('20160402 ', 'jk0402', 93); insert into score values ('20160301', 'jk0301 ', 80 ); insert into score values ('20170302', 'jk0302', 81); insert into score values ('20170302', 'jk0401', 82 ); insert into score values ('20170402 ', 'jk0402', 83); 4. Use the Wizard to generate a persistent unit. The process is the same as that in section 0511370104. 5. Use the Wizard to generate an object class. The process is the same as that in section 1st. 6. Relationship Analysis 1) one-to-one relationship between students and computers is a one-to-one relationship. In the student class, the code that represents the relationship is as follows: @ onetoone (cascade = cascadetype. all, mappedby = "student") private computer; the computer class has the following code indicating the relationship: @ joincolumn (name = "s_id", referencedcolumnname = "s_id ", insertable = false, updatable = false) @ onetoone private student; 2) there is a one-to-many relationship between students and scores. The student class has the following code indicating the relationship: @ onetoetype (cascade = cascadetype. all, mappedby = "student") private collection <score> scorecollection; 3) the relationship between multiple-to-one scores and students is one-to-one. The following code indicates the relation in the score class: @ joincolumn (name = "s_id", referencedcolumnname = "s_id", insertable = false, updatable = false) @ manytoone private student; 7. In the student table, the student ID and course number form a joint primary key. When creating an object class, a primary key class is required to identify the object. The primary key class code is as follows:/** scorepk. java ** created on May 30, 2007, ** to change this template, choose tools | template manager * and open the template in the editor. */package JPA; import Java. io. serializable; import javax. persistence. column; import javax. persistence. embeddable;/*** primary key class scorepk for entity class score *** @ author administrator */@ embeddablepublic class scorepk implements S Erializable {@ column (name = "s_id", nullable = false) Private string Sid; @ column (name = "c_id", nullable = false) Private string CID; /** creates a new instance of scorepk */Public scorepk () {}/ *** creates a new instance of scorepk with the specified values. * @ Param CID the cid of the scorepk * @ Param Sid the SID of the scorepk */Public scorepk (string CID, string Sid) {This. cid = CID; thi S. SID = Sid;}/*** gets the SID of this scorepk. * @ return the SID */Public String getsid () {return this. sid;}/*** sets the SID of this scorepk to the specified value. * @ Param Sid the new Sid */Public void setsid (string Sid) {This. SID = Sid;}/*** gets the CID of this scorepk. * @ return the CID */Public String getcid () {return this. CID;}/*** sets the CID of this scorepk to the SPE Cified value. * @ Param CID the new CID */Public void setcid (string CID) {This. cid = CID;}/*** returns a hash code value for the object. this implementation computes * a hash code value based on the ID fields in this object. * @ return a hash code value for this object. * // @ override public int hashcode () {int hash = 0; hash + = (this. CID! = NULL? This. CID. hashcode (): 0); hash + = (this. Sid! = NULL? This. sid. hashcode (): 0); Return hash;}/*** determines whether another object is equal to this scorepk. the result is * <code> true </code> if and only if the argument is not null and is a scorepk object that * has the same ID field values as this object. * @ Param object the reference object with which to compare * @ return <code> true </code> if this object is the same as the argument; * <code> Fals E </code> otherwise. * // @ override public Boolean equals (Object object) {// todo: Warning-This method won't work in the case the ID fields are not set if (! (Object instanceof scorepk) {return false;} scorepk Other = (scorepk) object; If (this. CID! = Other. CID & (this. cid = NULL |! This. CID. Equals (other. CID) return false; If (this. Sid! = Other. Sid & (this. Sid = NULL |! This. sid. equals (Other. sid) return false; return true;}/*** returns a string representation of the object. this implementation constructs * That representation based on the ID fields. * @ return a string representation of the object. * // @ override Public String tostring () {return "JPA. scorepk [cid = "+ CID +", SID = "+ Sid +"] ";}}

 

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.