Java-manipulating databases with Hibernate annotations

Source: Internet
Author: User

The operating environment of this program: idea.

To write a hibernate.cfg.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 ">

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

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.