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