Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

hibernate

  • Loading branch information...
commit 668e096404e618ac87cd4f4438c95072331ac333 1 parent 1d224f7
student authored
View
53 .classpath
@@ -1,26 +1,27 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<classpath>
- <classpathentry kind="src" output="target/classes" path="src/main/java">
- <attributes>
- <attribute name="optional" value="true"/>
- <attribute name="maven.pomderived" value="true"/>
- </attributes>
- </classpathentry>
- <classpathentry kind="src" output="target/test-classes" path="src/test/java">
- <attributes>
- <attribute name="optional" value="true"/>
- <attribute name="maven.pomderived" value="true"/>
- </attributes>
- </classpathentry>
- <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
- <attributes>
- <attribute name="maven.pomderived" value="true"/>
- </attributes>
- </classpathentry>
- <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
- <attributes>
- <attribute name="maven.pomderived" value="true"/>
- </attributes>
- </classpathentry>
- <classpathentry kind="output" path="target/classes"/>
-</classpath>
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" output="target/classes" path="src/main/java">
+ <attributes>
+ <attribute name="optional" value="true"/>
+ <attribute name="maven.pomderived" value="true"/>
+ </attributes>
+ </classpathentry>
+ <classpathentry kind="src" output="target/test-classes" path="src/test/java">
+ <attributes>
+ <attribute name="optional" value="true"/>
+ <attribute name="maven.pomderived" value="true"/>
+ </attributes>
+ </classpathentry>
+ <classpathentry kind="src" path="src/main/resources"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
+ <attributes>
+ <attribute name="maven.pomderived" value="true"/>
+ </attributes>
+ </classpathentry>
+ <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
+ <attributes>
+ <attribute name="maven.pomderived" value="true"/>
+ </attributes>
+ </classpathentry>
+ <classpathentry kind="output" path="target/classes"/>
+</classpath>
View
36 pom.xml
@@ -12,9 +12,45 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <org.hibernate.version>3.6.6.Final</org.hibernate.version>
</properties>
<dependencies>
+
+ <dependency>
+ <groupId>org.hsqldb</groupId>
+ <artifactId>hsqldb</artifactId>
+ <version>2.2.4</version>
+ </dependency>
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-core</artifactId>
+ <version>${org.hibernate.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-annotations</artifactId>
+ <version>3.4.0.GA</version>
+ </dependency>
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-commons-annotations</artifactId>
+ <version>3.2.0.Final</version>
+ </dependency>
+ <!-- if we need JPA -->
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-entitymanager</artifactId>
+ <version>${org.hibernate.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-validator</artifactId>
+ <version>4.2.0.Final</version>
+</dependency>
+
+
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
View
37 src/main/java/org/example/App.java
@@ -1,5 +1,12 @@
package org.example;
+import org.example.domain.Address;
+import org.example.domain.Person;
+import org.example.domain.User;
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+import org.hibernate.cfg.Configuration;
+
/**
* Hello world!
*
@@ -8,6 +15,34 @@
{
public static void main( String[] args )
{
- System.out.println( "Hello World!" );
+ SessionFactory factory = new Configuration().configure().buildSessionFactory();
+ Session session = factory.openSession();
+
+ Person p = new Person();
+ p.setName("Jan");
+ p.setSurname("Kowalski");
+ User u = new User();
+ u.setUserName("jkowal");
+ u.setPassword("password");
+ u.setPerson(p);
+ p.setUser(u);
+ p.setNationalIdentificationNumber("1234567890");
+
+ Address a = new Address();
+ a.setCity("Gdańsk");
+ a.setHouseNumber("55");
+ a.setStreet("Brzegi");
+ a.setLocalNumber("1");
+ a.setPostalCode("80-300");
+ a.setPerson(p);
+ p.getAddresses().add(a);
+
+ session.beginTransaction();
+
+ session.save(p);
+ session.save(u);
+ session.save(a);
+ session.getTransaction().commit();
+
}
}
View
10 src/main/java/org/example/domain/Address.java
@@ -1,7 +1,16 @@
package org.example.domain;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.ManyToOne;
+
+@Entity
public class Address {
+ @Id
+ @GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String city;
private String street;
@@ -9,6 +18,7 @@
private String localNumber;
private String postalCode;
+ @ManyToOne
private Person person;
View
23 src/main/java/org/example/domain/Person.java
@@ -5,14 +5,35 @@
import java.util.HashSet;
import java.util.Set;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+import javax.persistence.OneToOne;
+import javax.persistence.Table;
+import javax.persistence.Transient;
+
+@Entity
+@Table(name="Osoby")
public class Person {
-
+
+ @Id
+ @GeneratedValue(strategy=GenerationType.AUTO)
private int id;
+
+ @Column(name="imie")
private String name;
+
+ @Column(name="nazwisko")
private String surname;
+ @Column(name="pesel")
private String nationalIdentificationNumber;
+ @OneToMany(mappedBy="person")
private Collection<Address> addresses = new ArrayList<Address>();
+ @OneToOne
private User user;
View
2  src/main/java/org/example/domain/Role.java
@@ -14,8 +14,6 @@
private Collection<Privilege> privileges = new ArrayList<Privilege>();
private Collection<User> users = new ArrayList<User>();
-
-
public Collection<Privilege> getPrivileges() {
return privileges;
}
View
14 src/main/java/org/example/domain/User.java
@@ -5,13 +5,25 @@
import java.util.HashSet;
import java.util.Set;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.OneToOne;
+import javax.persistence.Transient;
+
+@Entity
public class User {
+ @Id
+ @GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String userName;
private String password;
-
+ @OneToOne
private Person person;
+
+ @Transient
private Collection<Role> roles = new ArrayList<Role>();
public int getId() {
View
27 src/main/resources/hibernate.cfg.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!DOCTYPE hibernate-configuration PUBLIC
+ "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
+ "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
+
+<hibernate-configuration>
+ <session-factory>
+ <!-- Polaczenie do DB -->
+ <property name="connection.driver_class">org.hsqldb.ClientConnection</property>
+ <property name="connection.url">jdbc:hsqldb:hsql://localhost/workdb</property>
+
+ <!-- connection pool -->
+
+ <property name="connection.pool">1</property>
+
+ <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
+
+ <property name="show_sql">true</property>
+
+ <property name="hbm2ddl.auto">create</property>
+
+ <mapping class="org.example.domain.Person"/>
+ <mapping class="org.example.domain.User"/>
+ <mapping class="org.example.domain.Address"/>
+ </session-factory>
+</hibernate-configuration>
Please sign in to comment.
Something went wrong with that request. Please try again.