Skip to content

Instantly share code, notes, and snippets.

@dimontop27
Last active May 9, 2023 09:45
Show Gist options
  • Save dimontop27/4d8180a2165ed780ac1ee67891312104 to your computer and use it in GitHub Desktop.
Save dimontop27/4d8180a2165ed780ac1ee67891312104 to your computer and use it in GitHub Desktop.
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
/**
* Created by Ариорх on 27.05.2017.
*/
public class HibernateUtil {
private static SessionFactory sessionFactory = null;
static {
Configuration cfg = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties());
sessionFactory = cfg.buildSessionFactory(builder.build());
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
import org.hibernate.SessionFactory;
/**
* Created by Ариорх on 27.05.2017.
*/
public class Main {
public static void main(String[] args) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
}
}
<?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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="models.User"/>
<mapping class="models.Role"/>
<mapping class="models.Order"/>
<mapping class="models.Product"/>
<mapping class="models.ProductCategory"/>
</session-factory>
</hibernate-configuration>
package models;
import javax.persistence.*;
import java.io.Serializable;
@MappedSuperclass
public abstract class Model implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "Id")
private long id;
public Model() {
}
public Model(long id) {
this.id = id;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
package models;
import javax.persistence.*;
import java.math.BigDecimal;
/**
* Created by Ариорх on 28.05.2017.
*/
@Entity
@Table
public class Order extends Model {
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "user_id", referencedColumnName = "id")
private User user;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "product_id", referencedColumnName = "id")
private Product product;
@Column
private int count;
@Column
private BigDecimal price;
@Column
private Boolean status;
public Order() {
}
public Order(long id) {
super(id);
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Boolean getStatus() {
return status;
}
public void setStatus(Boolean status) {
this.status = status;
}
}
package models;
import javax.persistence.*;
import java.math.BigDecimal;
/**
* Created by Ариорх on 28.05.2017.
*/
@Entity
@Table(name = "product")
public class Product extends Model {
@Column
private String title;
@Column
private BigDecimal price;
@Column
private String description;
@ManyToOne (fetch = FetchType.LAZY)
@JoinColumn(name = "product_category_id", referencedColumnName = "id")
private ProductCategory productCategory;
public Product() {
}
public Product(long id) {
super(id);
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public ProductCategory getProductCategory() {
return productCategory;
}
public void setProductCategory(ProductCategory productCategory) {
this.productCategory = productCategory;
}
}
package models;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
/**
* Created by Ариорх on 28.05.2017.
*/
@Entity
@Table(name = "product_category")
public class ProductCategory extends Model {
@Column
private String title;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "parent_product_category", referencedColumnName = "id")
private ProductCategory parentProductCategory;
@OneToMany(mappedBy = "productCategory")
private Set<Product> products = new HashSet<Product>();
public ProductCategory() {
}
public ProductCategory(long id) {
super(id);
}
@OneToMany(mappedBy = "productCategory")
public Set<Product> getProducts() {
return products;
}
public void setProducts(Set<Product> products) {
this.products = products;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public ProductCategory getParentProductCategory() {
return parentProductCategory;
}
public void setParentProductCategory(ProductCategory parentProductCategory) {
this.parentProductCategory = parentProductCategory;
}
}
package models;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
/**
* Created by Ариорх on 27.05.2017.
*/
@Entity
@Table(name = "Role")
public class Role extends Model {
@Column(name = "Title")
private String title;
@ManyToMany(mappedBy = "roles")
private Set<User> users = new HashSet<User>();
public Role() {
super();
}
public Role(long id) {
super(id);
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
package models;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
/**
* Created by Ариорх on 27.05.2017.
*/
@Entity
@Table(name = "user")
public class User extends Model {
@Column
private int age;
@Column
private String firstName;
@Column
private String lastName;
@ManyToMany
@JoinTable(name = "user_role", joinColumns = {@JoinColumn(name = "user_id")},
inverseJoinColumns = {@JoinColumn(name = "role_id")})
private Set<Role> roles = new HashSet<Role>();
public User() {
super();
}
public User(long id) {
super(id);
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment