@OneToMany annotations can be used in a one-to-many data structure to manipulate the database, with classes <-> students as examples to test this annotation.
First take a look at the project structure:
XML file configuration, similar to the previous ' one-to-two ' essay, has two Java classes: classroom, Student. Therefore, you should write two tables to the mapping label in the XML file:
<?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 ">Classroom class:
Package O2m;import Javax.persistence.*;import Java.util.Set, @Entitypublic class Classroom { @Id @ Generatedvalue (strategy= generationtype.identity) @Column private Integer CID; @OneToMany (cascade = {Cascadetype.all}, Fetch = Fetchtype.lazy) @JoinColumn (name = "CID") private set< Student> Stus; Students set @Column private Integer nums; Total number of a class public classroom () { } public Classroom (set<student> stus, Integer nums) { This.stus = Stus; This.nums = nums; } Public Integer Getcid () { return cid; } public void Setcid (Integer cid) { this.cid = cid; } Public set<student> Getstus () { return stus; } public void Setstus (set<student> stus) { this.stus = stus; } Public Integer getnums () { return nums; } public void Setnums (Integer nums) { this.nums = nums; }}
@OneToMany annotations are written on the corresponding multiple heads in fewer classes.
Student class:
Package O2m;import Org.hibernate.annotations.genericgenerator;import Javax.persistence.*;import java.util.Date;@ Entitypublic class Student {@Id @GeneratedValue (generator= "Sid") @GenericGenerator (name= "Sid", strategy = "Assig Ned ") @Column (length = 6) private String SID; Study number @Column private String name; Name @Column private String sex; Sex @Column private String school; School @Column private String address; Address @Column private Date registertime; Registration time Public Student () {} public Student (string sid, String name, String sex, string school, string address, Date registertime) {this.sid = SID; THIS.name = name; This.sex = sex; This.school = School; this.address = address; This.registertime = Registertime; } public String GetSID () {return SID; } public void Setsid (String sid) {this.sid = SID; } public String GetName () { return name; } public void SetName (String name) {this.name = name; } public String Getsex () {return sex; public void Setsex (String sex) {this.sex = sex; } public String Getschool () {return school; } public void Setschool (String school) {This.school = school; } public String getaddress () {return address; The public void setaddress (String address) {this.address = address; Public Date Getregistertime () {return registertime; } public void Setregistertime (Date registertime) {this.registertime = Registertime; }}There is also a @manytoone annotation, @ManyToOne should be added in the number of classes in the corresponding less head, but here I do not use, because the current demand as long as one-way foreign key association.
So there are two tables:
Then insert the data test:
Package Main;import o2m. Classroom;import o2m. Student;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.Transaction;import Org.hibernate.boot.registry.standardserviceregistrybuilder;import Org.hibernate.cfg.configuration;import Org.hibernate.service.serviceregistry;import Controller. Studentcontroller;import java.util.*;p Ublic class Main {public static void main (string[] args) {Configuration Configuration = new configuration (). Configure (); Serviceregistry Registry = new Standardserviceregistrybuilder (). Configure (). build (); Sessionfactory sessionfactory = configuration.buildsessionfactory (registry); Session session = Sessionfactory.getcurrentsession (); Transaction Transaction = Session.begintransaction (); list<string> name = new Linkedlist<> (); list<string> sex = new linkedlist<> (); list<string> address = new linkedlist<> (); Name.add ("Vortex naruto"); NAme.add ("Yu Uchiha Help"); Name.add ("Spring Wild cherry Blossoms"); Name.add ("Flag of the Wood card West"); Name.add ("uninvited"); Name.add ("the Hand"); Name.add ("Great Snake Pill"); Name.add ("The Woo-wise ferret"); Name.add ("Nine Tails"); Name.add ("eight Tails"); Sex.add ("male"); Sex.add ("male"); Sex.add ("female"); Sex.add ("male"); Sex.add ("male"); Sex.add ("female"); Sex.add ("male"); Sex.add ("male"); Sex.add ("male"); Sex.add ("male"); Address.add ("No. No. 501, mu ye cun"); Address.add ("No. No. 504, mu ye cun"); Address.add ("No. No. 902, mu ye cun"); Address.add ("No. No. 401, mu ye cun"); Address.add ("No. 107th, Mu ye cun"); Address.add ("No. No. 219, mu ye cun"); Address.add ("No. No. 901, mu ye cun"); Address.add ("No. 116th, Mu ye cun"); Address.add ("No. No. 511, mu ye cun"); Address.add ("No. No. 801, mu ye cun"); set<student> S1 = new hashset<> (); for (int i = 0; i < name.size (); i++) {Student studenet = new Student ("S0000" + I, Name.get (i), sex.get (i), "Wood Leaf Village", Address.get (i), New Date ()); S1.add (studenet); Session.save (studenet); } Classroom C1 = new Classroom (S1, S1.size ()); Session.save (C1); Transaction.commit (); Session.close (); }}Note here that Session.close (), should be placed behind the commit (commit), or student in the CID is null, if not write can, but I think the General API is added to the standard a bit more compliant.
PS: Please do not care about my use of list, I know its bottom is linked list, certainly not an array of efficiency, here just want to use it only ...
Finally, take a look at the effect after inserting the data:
Classroom table:
The correspondence is: 1 class altogether 10 people.
Student table:
Obviously, the CID is the primary key of the foreign key classroom, that is, these students are in Class 1.
In fact, there are a lot of problems I do not understand, for example, I will be the CRUD function in a separate package to invoke, it can not have this effect ... Maybe my session is not using the correct, @OneToMany specific role I do not know ... I feel that there is no classroom object member in student, I do not manually set the operation, then the foreign key CID should not have a value (this is not correct, because the CID is the foreign key, since the classroom table created and to the CID assigned value, it should have value, It has nothing to do with student, but I am in a situation where the CID in the classroom class has a value, but the student table is null, such as the preceding Session.close () in the case of commit), Then it is only possible that @onetomany did this, and because of this annotation, the corresponding foreign key in the student table can be automatically correlated ... The feeling should be like this.
Use of java-@OneToMany