Make database "extra".
Author.java
package com.hometools.hibernate.library;
public class Author {
private Long id;
private String lastName;
private String firstname;
public Author(String firstname, String lastName) {
this.firstname = firstname;
this.lastName = lastName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
}
Book.java
package com.hometools.hibernate.library;
//import java.util.List;
import java.util.HashSet;
import java.util.Set;
public class Book {
private Long id;
private String title;
private Set<BookAuthor> bookAuthors;
public Book(String title) {
this.title = title;
this.bookAuthors = new HashSet<BookAuthor>();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Set<BookAuthor> getBookAuthors() {
return bookAuthors;
}
public void setBookAuthors(Set<BookAuthor> bookAuthors) {
this.bookAuthors = bookAuthors;
}
public void addBookAuthor(BookAuthor bookAuthor) {
this.bookAuthors.add(bookAuthor);
}
}
BookAuthor.java
package com.hometools.hibernate.library;
public class BookAuthor {
private Author author;
private Boolean primaryAuthor;
public BookAuthor(){
}
public BookAuthor(Author author, Boolean primaryAuthor) {
this.author = author;
this.primaryAuthor = primaryAuthor;
}
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
public Boolean getPrimaryAuthor() {
return primaryAuthor;
}
public void setPrimaryAuthor(Boolean primaryAuthor) {
this.primaryAuthor = primaryAuthor;
}
}
Main.java
package com.hometools.hibernate.library;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Main {
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
// TODO Auto-generated method stub
Book book = new Book("Hadoop Definitive Guide");
Author author1 = new Author("Yanish","Praganandha");
Author author2 = new Author("Abhishek","Math");
BookAuthor bookauthor1 = new BookAuthor(author1, true);
BookAuthor bookauthor2 = new BookAuthor(author2, false);
book.addBookAuthor(bookauthor1);
book.addBookAuthor(bookauthor2);
session.save(author1);
session.save(author2);
session.save(book);
session.getTransaction().commit();
session.close();
}
}
Author.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated May 4, 2015 7:04:14 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.hometools.hibernate.library.Author" table="AUTHOR">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="native" />
</id>
<property name="lastName" type="java.lang.String">
<column name="LASTNAME" />
</property>
<property name="firstname" type="java.lang.String">
<column name="FIRSTNAME" />
</property>
</class>
</hibernate-mapping>
Book.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated May 4, 2015 7:21:32 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.hometools.hibernate.library.Book" table="BOOK">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="native" />
</id>
<property name="title" type="java.lang.String">
<column name="TITLE" />
</property>
<set name="bookAuthors" table="BOOK_AUTHOR">
<key column="BOOK_ID"/>
<composite-element class="com.hometools.hibernate.library.BookAuthor">
<many-to-one name="author" column="AUTHOR_ID" class="com.hometools.hibernate.library.Author" />
<property name="primaryAuthor" type="java.lang.Boolean" column="PRIMARY_AUTHOR" />
</composite-element>
</set>
</class>
</hibernate-mapping>
hibernate-cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/extra</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="com/hometools/hibernate/library/Author.hbm.xml"/>
<mapping resource="com/hometools/hibernate/library/Book.hbm.xml"/>
</session-factory>
</hibernate-configuration>
No comments:
Post a Comment