Hibernate replaces a mapping file with annotations

Source: Internet
Author: User

1. First delete the original mapping file and add annotations to the entity Class:

@Entity//declares that the current class is hibernate-mapped to an entity class in the database@Table (name= "news")//declaring the name of the table public classNews {@Id//declares this column as the primary key, as the identifier for the mapped object    /*** @GeneratedValue Annotations to define a build strategy * Generationtype.tables The value of the current primary key is saved separately into a table in a database * generationtype.sequence leverage the underlying     The database provides the sequence generation identifier * Generationtype.identity takes the Database's self-increment policy * Generationtype.auto automatically chooses the appropriate ID generation scheme based on different databases, MySQL is used here for incremental */@GeneratedValue (strategy=generationtype.auto)PrivateInteger id; @Column (name= "title", nullable=false)    PrivateString title; @Column (name= "content", nullable=false)    PrivateString content; @Column (name= "begintime", nullable=false)    PrivateDate begintime; @Column (name= "username", nullable=false)    PrivateString username;  publicNews () {Super(); }         publicInteger getId () {returnid; }     public voidsetId (Integer Id) { this. ID =id; }         publicString getTitle () {returntitle; }     public voidsettitle (String Title) { this. title =title; }         publicString getcontent () {returncontent; }     public voidsetcontent (String Content) { this. Content =content; }         publicDate getbegintime () {returnbegintime; }     public voidsetbegintime (Date Begintime) { this. BeginTime =begintime; }         publicString getusername () {returnusername; }     public voidsetusername (String Username) { this. Username =username; }        }

To find out how hibernate annotations are used, refer to my previous blog post

Http://www.cnblogs.com/qq1272850043/p/5960365.html

2. After adding the annotations, go to the Applicationcontext.xml file to delete the property that found the corresponding mapping File.

<!--Delete This. -< propertyname= "mappingresources">    <List>        <value>News/entity/news.hbm.xml</value>    </List></ property>

3. Then add this in the Applicationcontext.xml.

<!--scan entity class packages to resolve annotations for entity classes -< propertyname= "packagestoscan">    <List>        <!--This value adds the package containing the entity class -        <value>News.entity</value>    </List></ property>

Hibernate replaces a mapping file with 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.