The operating environment of this program: idea.
<?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 ">Note that:
<property name= "Hibernate.connection.url" >jdbc:mysql://localhost:3306/hibernate?useSSL=false& Useunicode=true&characterencoding=utf-8</property>
The structure in this tag is: jdbc:mysql://host address/database name/Cancel SSL Authentication &unicode encoded &UTF-8 encoding
Add Usessl=false to remove the validation warning.
My primary key is in the students class:
Package Onetoone;import javax.persistence.*; @Entitypublic class Students { @Id @GeneratedValue (strategy= generationtype.identity) @Column private int id; @Column private String sex; @OneToOne (cascade = cascadetype.all) @JoinColumn (name = "Sid", unique = true) private Idcard Idcard; Public Students () { } public String getsex () { return sex; } public void Setsex (String sex) { this.sex = sex; } Public Idcard Getidcard () { return idcard; } public void Setidcard (Idcard idcard) { this.idcard = Idcard; } Public Students (String sex, Idcard idcard) { this.sex = sex; This.idcard = Idcard; }}
The primary key is the corresponding class member variable ID under @id, set to autogrow.
Foreign keys in Idcard:
Package Onetoone;import Org.hibernate.annotations.genericgenerator;import Javax.persistence.column;import Javax.persistence.entity;import Javax.persistence.generatedvalue;import javax.persistence.Id; @Entitypublic class Idcard {@Id @GeneratedValue (generator = "Sid") @GenericGenerator (name = "Sid", strategy = "assigned") @Column (length = 6) private int sid; @Column private String name; @Column Private String School; @Column private String address; Public Idcard () {} public idcard (int sid, String name, string school, string address) {this.sid = SID; THIS.name = name; This.school = School; this.address = address; } public int GetSID () {return SID; } public void Setsid (int sid) {this.sid = SID; } public String GetName () {return name; } public void SetName (String name) {this.name = name; } public String Getschool () {return school; } public voidSetschool (String school) {This.school = school; } public String getaddress () {return address; The public void setaddress (String address) {this.address = address; }}The foreign key is the class member Sid under @id, set to Manual setting.
Test Unit:
Package Main;import Org.hibernate.boot.metadata;import Org.hibernate.boot.metadatasources;import Org.hibernate.boot.registry.standardserviceregistrybuilder;import Org.hibernate.service.serviceregistry;import Org.hibernate.tool.hbm2ddl.schemaexport;import Org.hibernate.tool.schema.targettype;import Java.util.EnumSet; public class Main {public static void Main (string[] args) { Serviceregistry registry = new Standardserviceregistr Ybuilder (). Configure (). build (); Metadata Metadata = new Metadatasources (registry). Buildmetadata (); Schemaexport export = new Schemaexport (); Export.create (Enumset.of (targettype.database), metadata);} }
Test results using Navicat view:
Idcard:
Students
FOREIGN key:
Some of the hibernate core jar packages that need to be used:
Reference: Many ...
Java-manipulating databases with Hibernate annotations