Java Reference
In-Depth Information
6.
Make sure to include the Project class in the hibernate.cfg.xml file, as follows:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
" http://www.hibernate.org/dtd/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/employeeschema</property>
<property name="hibernate.connection.username">
root</property>
<property name="hibernate.connection.password">
mypassword123</property>
<property name="hibernate.connection.pool_size">
10</property>
<property name="show_sql">true</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">
thread</property>
<mapping class="Employee" />
<mapping class="Project" />
</session-factory>
</hibernate-configuration>
7.
Create a new class called myDBApp2 as follows:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import java.util.HashSet;
import java.util.Set;
public class myDBApp2 {
public static void main(String[] args) {
Set<Project> projects=new HashSet<Project>();
projects.add(new Project(1,"Hibernate Basic Project"));
projects.add(new Project(2, "Hibernate Many to Many Project"));
Employee Myemp=new Employee(7,"Hibernate freak", "Male", 1, projects );
SessionFactory sessionFactory =
new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
session.save(Myemp);
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
}
Search WWH ::




Custom Search