Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args) {
- SessionFactory sessionFactory = new HibernateUtil().getSessionFactory();
- Session session = sessionFactory.openSession();
- session.beginTransaction();
- Role role = new Role();
- role.setTitle("ex-president");
- User user1 = new User(100,"Barack","Obama");
- User user2 = new User(100,"Ronald","Reagan");
- user1.setRole(role);
- user2.setRole(role);
- session.save(user1);
- session.save(user2);
- role.getUsers().add(user1);
- role.getUsers().add(user2);
- session.save(role);
- session.getTransaction().commit();
- session.close();
- }}
- public class HibernateUtil {
- private static SessionFactory sessionFactory;
- static {
- Configuration configuration = new Configuration();
- configuration.configure("hibernate.cfg.xml");
- StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
- sessionFactory = configuration.buildSessionFactory(ssrb.build());
- }
- public SessionFactory getSessionFactory(){
- return sessionFactory;
- }}
- @Entity
- public class Role {
- private Long role_id;
- private String title;
- private Set<User> users = new HashSet<User>();
- public Role(){}
- public Set<User> getUsers() {
- return users;
- }
- public void addUser(User user){
- users.add(user);
- }
- public void setUsers(Set<User> users) {
- this.users = users;
- }
- public Long getRole_id() {
- return role_id;
- }
- public void setRole_id(Long id) {
- this.role_id = id;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }}
- @Entity
- public class User {
- private long user_id;
- private int age;
- private String firstname;
- private String lastname;
- private Role role;
- public User(){
- }
- public Role getRole() {
- return role;
- }
- public void setRole(Role role) {
- this.role = role;
- }
- public User(int age, String firstname, String lastname) {
- this.age = age;
- this.firstname = firstname;
- this.lastname = lastname;
- }
- public long getUser_id() {
- return user_id;
- }
- public void setUser_id(long id) {
- this.user_id = id;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getFirstname() {
- return firstname;
- }
- public void setFirstname(String firstname) {
- this.firstname = firstname;
- }
- public String getLastname() {
- return lastname;
- }
- public void setLastname(String lastname) {
- this.lastname = lastname;
- }}
- <?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="connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
- <property name="connection.username">root</property>
- <property name="connection.password">root</property>
- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
- <property name="hbm2ddl.auto">create</property>
- <property name="show_sql">true</property>
- <property name="format_sql">true</property>
- <mapping resource="user.cfg.xml"/>
- <mapping resource="role.cfg.xml"/>
- </session-factory>
- </hibernate-configuration>
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD//EN"
- "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="Role" table="role">
- <id name="role_id" column="id">
- <generator class="native"/>
- </id>
- <property name="title" column="TITLE"/>
- <set name="users" cascade="all"
- inverse="true" lazy="true" fetch="select">
- <key column="id" not-null="true"/>
- <one-to-many class="User"/>
- </set>
- </class>
- </hibernate-mapping>
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD//EN"
- "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="User" table="user">
- <id name="user_id" column="user_id">
- <generator class="native"/>
- </id>
- <property name="firstname" column="firstname"/>
- <property name="lastname" column="lastname"/>
- <property name="age" column="age"/>
- <many-to-one name="role" class="Role" fetch="select">
- <column name="id" not-null="true"/>
- </many-to-one>
- </class>
- </hibernate-mapping>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement