Skip to content

Commit

Permalink
Merge pull request #1 from altaec23/less-hiber
Browse files Browse the repository at this point in the history
Less hiber
  • Loading branch information
altaec23 authored Apr 10, 2022
2 parents dcfef1d + 70996b3 commit 95b45a8
Show file tree
Hide file tree
Showing 35 changed files with 811 additions and 98 deletions.
5 changes: 2 additions & 3 deletions HiberModule/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}


group 'org.example'

version '1.0-SNAPSHOT'

compileJava.options.encoding = "UTF-8"
Expand All @@ -27,11 +27,10 @@ dependencies {

implementation('org.slf4j:slf4j-api:1.7.30')

// https://mvnrepository.com/artifact/org.bgee.log4jdbc-log4j2/log4jdbc-log4j2-jdbc4.1
implementation group: 'org.bgee.log4jdbc-log4j2', name: 'log4jdbc-log4j2-jdbc4.1', version: '1.16'

}


test {
useJUnitPlatform()
}
45 changes: 0 additions & 45 deletions HiberModule/src/main/java/core/Main.java

This file was deleted.

41 changes: 41 additions & 0 deletions HiberModule/src/main/java/example1/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package example1;


import example1.dao.CarEmDao;
import example1.model.Car;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;


/**
* @author alekseev.a
* @since 1.0
*/
public class Main {

public static CarEmDao carDao;
private static final Logger log = LoggerFactory.getLogger(Main.class);

public static void main(String[] args) {
createAnnotationContext();
useCarDao();
}

public static void createAnnotationContext() {
log.info("Hello");
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("first_unit");
carDao = new CarEmDao(entityManagerFactory);
}

static void useCarDao() {
Car car = new Car("niva");
carDao.saveCar(car);

}


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package core;
package example1;

import core.model.User;
import example1.model.User;

import java.sql.*;
import java.util.ArrayList;
Expand Down
130 changes: 130 additions & 0 deletions HiberModule/src/main/java/example1/dao/CarDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package example1.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import example1.model.Car;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.function.Consumer;

public class CarDao {
public static final Logger logger = LoggerFactory.getLogger(CarDao.class);

private final SessionFactory sessionFactory;

public CarDao(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

public static CarDao create() {
return create("hibernate.annotation.cfg.xml");
}

// Method 1: This Method Used To Create A New Car Record In The Database Table
public Car createRecord(Car car) {
Session session = sessionFactory.openSession();

//Creating Transaction Object
Transaction transaction = session.beginTransaction();
session.save(car);

// Transaction Is Committed To Database
transaction.commit();

// Closing The Session Object
session.close();
logger.info("Successfully Created " + car.toString());
return car;
}

public static CarDao create(String configuration) {
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure(configuration).build();
Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();
return new CarDao(meta.getSessionFactoryBuilder().build());
}



// Method 2: This Method Is Used To Display The Records From The Database Table
public List<Car> findAll() {
Session session = sessionFactory.openSession();
List<Car> cars = session.createQuery("FROM Car", Car.class).list();

// Closing The Session Object
session.close();
logger.info("Car Records Available In Database Are?= " + cars.size());
return cars;
}

// Method 3: This Method Is Used To Update A Record In The Database Table
public Car updateRecord(Car car) {
Session session = sessionFactory.openSession();

//Creating Transaction Object
Transaction transaction = session.beginTransaction();
Car savedCar = session.load(Car.class, car.getId());
savedCar.setModel(car.getModel());

// Transaction Is Committed To Database
transaction.commit();

// Closing The Session Object
session.close();
logger.info("Car Record Is Successfully Updated!= " + car.toString());
return savedCar;
}

// Method 4(a): This Method Is Used To Delete A Particular Record From The Database Table
public void deleteRecord(Integer id) {
Session session = sessionFactory.openSession();

//Creating Transaction Object
Transaction transObj = session.beginTransaction();
Car car = session.load(Car.class, id);
session.delete(car);

// Transaction Is Committed To Database
transObj.commit();

// Closing The Session Object
session.close();
logger.info("Successfully Record Is Successfully Deleted!= " + car.toString());

}

// Method 4(b): This Method To Find Particular Record In The Database Table
public Car findRecordById(Integer id) {
Session session = sessionFactory.openSession();
Car car = session.get(Car.class, id);
// Closing The Session Object
session.close();

return car;
}

public void acceptToAll(Consumer<Car> consumer) {
Session session = sessionFactory.openSession();
List<Car> cars = session.createQuery("FROM Car", Car.class).list();

Transaction transaction = session.beginTransaction();
try {
cars.forEach(car -> {
consumer.accept(car);
session.merge(car);
});
transaction.commit();
} catch (Exception e) {
transaction.rollback();
}

// Closing The Session Object
session.close();
}
}
21 changes: 21 additions & 0 deletions HiberModule/src/main/java/example1/dao/CarEmDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package example1.dao;

import example1.model.Car;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

public class CarEmDao {
private static EntityManager em;

public CarEmDao(EntityManagerFactory entityManagerFactory) {
em = entityManagerFactory.createEntityManager();
}

public void saveCar(Car car) {
em.getTransaction().begin();
em.persist(car);
em.getTransaction().commit();

}
}
47 changes: 47 additions & 0 deletions HiberModule/src/main/java/example1/model/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package example1.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import static javax.persistence.GenerationType.IDENTITY;

@Entity
@Table(name = "car")
public class Car {
@Id
@GeneratedValue(strategy = IDENTITY)
private Integer id;

@Column(length = 100)
private String model;

public Car() {
}

public Car(String model) {
this.model = model;
}

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}

public Integer getId() {
return id;
}

@Override
public String toString() {
return "Car{" +
"id=" + id +
", model='" + model + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core.model;
package example1.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core.model;
package example1.model;

/**
* @author alekseev.a
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core.model;
package example1.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down
52 changes: 52 additions & 0 deletions HiberModule/src/main/java/example2/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package example2;

import example1.dao.CarEmDao;
import example2.dao.DisciplineDao;
import example2.dao.GroupDao;
import example2.dao.StudentDao;
import example2.model.ContactInfo;
import example2.model.Discipline;
import example2.model.Group;
import example2.model.Student;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.util.List;
import java.util.Set;

public class Main {
private static StudentDao studentDao;
private static DisciplineDao disciplineDao;
private static GroupDao groupDao;

public static void main(String[] args) {
createAnnotationContext();
// saveData();
List<Student> named = studentDao.findAllWithContactNamed();
}

static void createAnnotationContext() {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("second_unit");
studentDao = new StudentDao(entityManagerFactory);
disciplineDao = new DisciplineDao(entityManagerFactory);
groupDao = new GroupDao(entityManagerFactory);
}

static void saveData() {
Discipline discipline1 = Discipline.builder().code(3).description("disc3").build();
Discipline discipline2 = Discipline.builder().code(4).description("disc4").build();
disciplineDao.save(discipline1);
disciplineDao.save(discipline2);

Group group = Group.builder().name("group3").build();
groupDao.save(group);

ContactInfo contactInfo = ContactInfo.builder().email("asdaas").telephoneNumber("numxber").build();
Student student = Student.builder().name("n1ame").contact(contactInfo)
.s_group(group)
.disciplines(Set.of(discipline1, discipline2))
.build();
studentDao.save(student);

}
}
Loading

0 comments on commit 95b45a8

Please sign in to comment.