Hibernate using annotation methods

Source: Internet
Author: User
Tags generator
------------------Annotations-------------------@Entity//DECLARE an entity @Table (name= "table name")//with table mappings @Id//Specify the PRIMARY key @Column//Specify the column corresponding to the list







-----------Many-to-many annotation method programming step-----------First, we need to build three tables, a table of two JavaBean classes, and an intermediate table to build the table notes:
1, the corresponding JavaBean of the two tables must have a primary key, these two primary keys corresponding to the two foreign keys in the middle table, so as to establish the association
2. Examples of SQL statements are as follows: CREATE TABLE Student2 (ID number primary key NOT NULL, name varchar (30));
CREATE TABLE course (ID number primary key not NULL, name VARCHAR2 (30));
CREATE TABLE Student2_course (s_id number references Student2 (ID), c_id number references course (ID));
Second, the use of annotations (in front of the class note) 1, @Entity//placed in front of the JavaBean class to declare that this is an entity 2, @Table (name= "table name")//placed in @Entity, to declare the entity class corresponding to table 3, @Gener Icgenerator (name = "GenID", strategy = "increment")//declares how the primary key is generated
(Note Before method name) 4, @Id          //declares a primary key, or it can be placed before the Get method 5, @Column//declares the column in the corresponding table of the variable   6, @GeneratedValu E (generator= "GenID")   //and genericgenerator corresponding to use the value of the generator property inside the   is the same value as the name in Genericgenerator 7, @ Manytomany          //Statement Many-to-many relationship 8, @JoinTable (name= "Student2_course",       &NBSP ;                          ,         &NB sp;                           joincolumns={@JoinColumn (name= "c_id")},                      inversejoincolumns={@JoinColumn (name= "s_id")})                    //n AME: corresponding intermediate table                                               ,         &NB Sp                          ,         &NB Sp                              //joncolumns: The Inside reference The number is the array, so the table associated with this JavaBean primary key is declared with {} @joincolumn (name= "") foreign key                 &NBSP ;                          ,         &NB Sp                          ,         &NB Sp                              //inversejoincolum NS: In the same vein, declare the foreign key in the table with another JavaBean pair             Specific code examples are as follows:           &NBS P &nbsP                          ,         &NB Sp                          ,         &NB Sp   //Class Coursu total @Entity @Table (name= "course") @GenericGenerator (name = "GenID", strategy = "increment") public c Lass Course {      @Id       @GeneratedValue (generator= "GenID")       @Column (name= "id")      private Long ID;       @Column (name= "name")      private String name;       @ManyToMany       @JoinTable (name= "Student2_course",                     joincolumns={@JoinColumn (name= "c_id")},                    inversejoincolumns={@JoinColumn (name= "s_id")})      private set<student2> students = new hashset<student2> ();

Third, the configuration file Hibernate.cfg.xml need to add the mapping class in the configuration file <mapping class= "The path of the corresponding class/> <mapping class=" Com.ldl . domain1. Goodbean "/>
<! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://www.hibernate. Org/dtd/hibernate-configuration-3.0.dtd ">
<!--      1, load Database driver, get database connection-     <session-factory >           <property name= "Hibernate.dialect" > Org.hibernate.dialect.oracle9idialect</property>            <property name= "Hibernate.connection.driver_class" >oracle.jdbc.driver.OracleDriver</property>           <property name= "Hibernate.connection.username" >scott</property>           <property name= " Hibernate.connection.password ">tiger</property>            <property name= "Hibernate.connection.url" >jdbc:oracle:thin: @localhost:1521:orcl</property> <!--169.254.146.196--          <!--show SQL, default to False-            <property name= "Hibernate.show_sql" >true</ Property>
<!--adding mapping classes--<mapping class= "Com.ldl.domain1.GoodBean"/> <mapping class= "Com.ldl.domai N2. Course "/> <mapping class=" Com.ldl.domain2.Student2 "/>
</session-factory>

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.