Hibernate 4th-hibernate _ set ing

Source: Internet
Author: User

Hibernate set ing Demo:

Prerequisites:

Book [ID, name, authors], there may be n authors, so set is used here;

The object class has one book. There are two tables: tbook and tauthors;

1: Model:

package model;import java.util.Set;public class Book {private int id;private String name;private Set<String> authors;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Set<String> getAuthors() {return authors;}public void setAuthors(Set<String> authors) {this.authors = authors;}}

2: ing file of book: HBM. xml

<class name="Book" table="TBook" lazy="false">        <id name="id">            <generator class="native"/>        </id>        <property name="name" column="bookName"/>        <set name="authors" table="TAuthors">        <key column="id" />        <element column="authorName" type="string" not-null="true" length="30"/>        </set>    </class>

Note the following two points:

* State that not-null = "true" will automatically generate the primary key [ID, authorname] for you only when the tauthors table is generated; otherwise, the table has no primary key;

* If length = "30" is not specified, the following error occurs. Primary Key Generation fails. Therefore, you must specify a smaller value;

 not-null="true" length="30"
1218 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate - Unsuccessful: create table TAuthors (id integer not null, topicName varchar(255) not null, primary key (id, topicName))1218 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate - Specified key was too long; max key length is 767 bytes

3. Dao:

/** add */public void addPerson(Book p){Session session=null;Transaction tran=null;try {session=HibernateUtil.getSession();tran=session.beginTransaction();session.save(p);tran.commit();} catch (HibernateException e) {tran.rollback();e.printStackTrace();}finally{if(session!=null && session.isOpen()){session.close();}}}

4: Service:

        BookDao dao=new BookDao();/** add */public void add(Book p){dao.addPerson(p);}

5: JUnit test:

@ Test public void testadd () {Book p = New Book (); p. setname ("Java core technology"); set <string> authors = new hashset <string> (); authors. add ("cay S. horstmann "); authors. add ("Gary Cornell"); p. setauthors (authors); manager. add (p );}

6: final database result:

Mysql> select * From tbook; + ---- + ------------------ + | ID | bookname | + ---- + ------------------ + | 1 | Java core technology | + ---- + ------------------ + 1 row in setmysql> select * From tauthors; + ---- + --------------- + | ID | authorname | + ---- + ----------------- + | 1 | Cay S. horstmann | 1 | Gary Cornell | + ---- + --------------- + 2 rows in Set

DDL statement of the table:

CREATE TABLE `tbook` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `bookName` varchar(255) DEFAULT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8--------------------------------------------------------------------CREATE TABLE `tauthors` (  `id` int(11) NOT NULL,  `authorName` varchar(30) NOT NULL,  PRIMARY KEY (`id`,`authorName`),  KEY `FK529261547AAF8BC9` (`id`),  CONSTRAINT `FK529261547AAF8BC9` FOREIGN KEY (`id`) REFERENCES `tbook` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8

That's all;

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.