Hibernate annotation case

Source: Internet
Author: User
In the previous process of getting hibernate, every time you create a database table, you must create a corresponding *. hbm. xml configuration file under entity. When there are many database tables, this will seriously affect the opening

In the previous process of getting hibernate, every time you create a database table, you must create a corresponding *. hbm. xml configuration file under entity. When there are many database tables, this will seriously affect the opening

I. Introduction

In the previous steps of getting hibernate, every time you create a database table, you must create a corresponding * under entity *. hbm. xml configuration file. When there are many database tables, it will seriously affect the development efficiency and maintenance costs, so there is an annotation, the principle is to save the tedious configuration file, instead of adding a tag starting with "@" in the object class, the annotation of life will be explained one by one:

2. Preparation

Jar file:

Hibernate-core-3.3.2.GA.jar

Ejb3-persistence-1.0.2.GA.jar

Hibernate-annotations-3.4.0.GA.jar

Hibernate-commons-annotations-3.1.0.GA.jar

Javassist-3.11.0.GA.jar

(Indispensable)

Iii. Implementation

Package com. hibernate. entity;

Import java. io. Serializable;

Import javax. persistence. Column;
Import javax. persistence. Entity;
Import javax. persistence. FetchType;
Import javax. persistence. GeneratedValue;
Import javax. persistence. GenerationType;
Import javax. persistence. Id;
Import javax. persistence. JoinColumn;
Import javax. persistence. JoinTable;
Import javax. persistence. ManyToOne;
Import javax. persistence. SequenceGenerator;
Import javax. persistence. Table;


/**
* Movie details
* @ Author Administrator
*
*/
@ Entity // mark as a database table
@ Table (name = "tb_film_info") // specifies the database Table name mapped to the object class.
Public class FilmInfo implements Serializable {

Private static final long serialVersionUID = 1L;

Private Long fiId;
Private String fiName;
Private String fiActor;
Private String fiDirector;
Private Double fiTicketPrice;

Private FilmType filmType;

@ Id // primary key id
@ SequenceGenerator (name = "generator", sequenceName = "seq_film_info") // specify the sequence
@ GeneratedValue (generator = "generator", strategy = GenerationType. SEQUENCE) // specify the primary key generation policy.
@ Column (name = "fi_id") // because the structure in the data table is separated by "_", hibernate does not recognize the structure in any way by default, therefore, each column must be marked in the getter () method.
Public Long getFiId (){
Return fiId;
}
Public void setFiId (Long fiId ){
This. fiId = fiId;
}
@ Column (name = "fi_name ")
Public String getFiName (){
Return fiName;
}
Public void setFiName (String fiName ){
This. fiName = fiName;
}
@ Column (name = "fi_actor ")
Public String getFiActor (){
Return fiActor;
}
Public void setFiActor (String fiActor ){
This. fiActor = fiActor;
}
@ Column (name = "fi_director ")
Public String getFiDirector (){
Return fiDirector;
}
Public void setFiDirector (String fiDirector ){
This. fiDirector = fiDirector;
}
@ Column (name = "fi_ticket_price ")
Public Double getFiTicketPrice (){
Return fiTicketPrice;
}
Public void setFiTicketPrice (Double fiTicketPrice ){
This. fiTicketPrice = fiTicketPrice;
}
@ ManyToOne (fetch = FetchType. LAZY) // multiple-to-one association, marked with @ ManyToOne, specifying the Loading Mode
@ JoinColumn (name = "fi_film_type_id") // specify the associated foreign key
Public FilmType getFilmType (){
Return filmType;
}
Public void setFilmType (FilmType filmType ){
This. filmType = filmType;
}
}

Package com. hibernate. entity;

Import java. io. Serializable;
Import java. util. ArrayList;
Import java. util. List;

Import javax. persistence. CascadeType;
Import javax. persistence. Column;
Import javax. persistence. Entity;
Import javax. persistence. FetchType;
Import javax. persistence. GeneratedValue;
Import javax. persistence. GenerationType;
Import javax. persistence. Id;
Import javax. persistence. JoinColumn;
Import javax. persistence. onetoence;
Import javax. persistence. SequenceGenerator;
Import javax. persistence. Table;

/**
* Movie type
* @ Author Administrator
*
*/
@ Entity // mark as a database table
@ Table (name = "tb_film_type") // specifies the database Table name mapped to the object class
Public class FilmType implements Serializable {

Private static final long serialVersionUID = 1L;

Private Long ftId;
Private String ftName;

Private List FilmInfos = new ArrayList ();

@ Id
@ SequenceGenerator (name = "generator", sequenceName = "seq_film_type ")
@ GeneratedValue (generator = "generator", strategy = GenerationType. SEQUENCE)
@ Column (name = "ft_id ")
Public Long getFtId (){
Return ftId;
}
Public void setFtId (Long ftId ){
This. ftId = ftId;
}
@ Column (name = "ft_name ")
Public String getFtName (){
Return ftName;
}
Public void setFtName (String ftName ){
This. ftName = ftName;
}

@ OneToMany (fetch = FetchType. LAZY, cascade = CascadeType. ALL) // one-to-many Association, specifying the cascade Method
@ JoinColumn (name = "fi_film_type_id") // specify the primary key id of the tb_Film_info table and the foreign key that references the tb_film_type table.
Public List GetFilmInfos (){
Return filmInfos;
}
Public void setFilmInfos (List FilmInfos ){
This. filmInfos = filmInfos;
}

}

This is basically what it looks like. In the past, the way to get SessionFactory through hibernate ing file Configuration was the Configuration instance, but the AnnotationConfiguration instance should be used after hibernate annotation is used, as shown below:

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.