-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from altaec23/less-hiber
Less hiber
- Loading branch information
Showing
35 changed files
with
811 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
|
||
|
||
} |
4 changes: 2 additions & 2 deletions
4
...rc/main/java/core/SimpleBDConnection.java → ...ain/java/example1/SimpleBDConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + '\'' + | ||
'}'; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...Module/src/main/java/core/model/City.java → ...le/src/main/java/example1/model/City.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...le/src/main/java/core/model/CityType.java → ...rc/main/java/example1/model/CityType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package core.model; | ||
package example1.model; | ||
|
||
/** | ||
* @author alekseev.a | ||
|
2 changes: 1 addition & 1 deletion
2
...Module/src/main/java/core/model/User.java → ...le/src/main/java/example1/model/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} |
Oops, something went wrong.