Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
# Compiled class file
*.class

Expand Down
1 change: 1 addition & 0 deletions 1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
orphanRemoval
135 changes: 135 additions & 0 deletions full.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
BEGIN;

DROP TABLE IF EXISTS employees_details CASCADE;
CREATE TABLE employees_details (id bigserial PRIMARY KEY, email VARCHAR(255), city varchar(255));
INSERT INTO employees_details (email, city) VALUES
('terminator@gmail.com', 'California'),
('rembo@gmail.com', 'Atlanta'),
('corben_dallas@gmail.com', 'New York');

DROP TABLE IF EXISTS employees CASCADE;
CREATE TABLE employees (id bigserial PRIMARY KEY, name VARCHAR(255), details_id bigint, FOREIGN KEY (details_id) REFERENCES employees_details (id));
INSERT INTO employees (name, details_id) VALUES
('Arnold S.', 1),
('Silvester S.', 2),
('Willis B.', 3);

DROP TABLE IF EXISTS simple_items CASCADE;
CREATE TABLE simple_items (id bigserial PRIMARY KEY, title VARCHAR(255), price int);
INSERT INTO simple_items (title, price) VALUES
('box', 10),
('sphere', 20),
('maul', 100),
('door', 50),
('camera', 500);

DROP TABLE IF EXISTS books CASCADE;
CREATE TABLE books (id bigserial PRIMARY KEY, title VARCHAR(255));
INSERT INTO books (title) VALUES
('Mistborn'),
('Neverwhere'),
('Ambers Chronicles'),
('Harry Potter'),
('Lockwood & Co.'),
('Foundation Trilogy'),
('Liveship Traders Trilogy'),
('A Night in the Lonesome October'),
('Da Vinci Code'),
('Lord of the Ring');

DROP TABLE IF EXISTS readers CASCADE;
CREATE TABLE readers (id bigserial PRIMARY KEY, name VARCHAR(255));
INSERT INTO readers (name) VALUES
('Alexander'),
('Bob');

DROP TABLE IF EXISTS books_readers CASCADE;
CREATE TABLE books_readers (book_id bigint, reader_id bigint, FOREIGN KEY (book_id) REFERENCES books (id), FOREIGN KEY (reader_id) REFERENCES readers (id));
INSERT INTO books_readers (book_id, reader_id) VALUES
(1, 1),
(2, 1),
(3, 1),
(4, 1),
(5, 1),
(6, 1),
(7, 1),
(8, 1),
(9, 1),
(10, 1),
(1, 2),
(2, 2);

DROP TABLE IF EXISTS universities CASCADE;
CREATE TABLE universities (id bigserial PRIMARY KEY, title VARCHAR(255));
INSERT INTO universities (title) VALUES
('DSTU'),
('NPU');

DROP TABLE IF EXISTS students CASCADE;
CREATE TABLE students (id bigserial PRIMARY KEY, name VARCHAR(255), university_id bigint REFERENCES universities (id));
INSERT INTO students (name, university_id) VALUES
('Alexander', 1),
('Bob', 2),
('John', 1);

DROP TABLE IF EXISTS validation_beans CASCADE;
CREATE TABLE validation_beans (id bigserial PRIMARY KEY, email VARCHAR(255), priority int DEFAULT 5, postal_code varchar(6), created_at timestamp, updated_at timestamp);
INSERT INTO validation_beans (email, priority, postal_code) VALUES
('data@mail.ru', 5, '100000');

DROP TABLE IF EXISTS items CASCADE;
CREATE TABLE items (id bigserial PRIMARY KEY, title VARCHAR(255), price numeric(6, 2));
INSERT INTO items (title, price) VALUES
('milk', 79.90),
('bread', 24.90),
('butter', 220.00),
('cheese', 350.55),
('coca-cola', 69.90);

DROP TABLE IF EXISTS passports CASCADE;
CREATE TABLE passports (pserial int, pnumber int, registration_address VARCHAR(255), PRIMARY KEY (pserial, pnumber));
INSERT INTO passports (pserial, pnumber, registration_address) VALUES
(6080, 965400, 'country.state.city.street');

DROP TABLE IF EXISTS citizens CASCADE;
CREATE TABLE citizens (id bigserial, name varchar(255), passport_serial int, passport_number int, CONSTRAINT fk_passport FOREIGN KEY (passport_serial, passport_number) REFERENCES passports (pserial, pnumber));
INSERT INTO citizens (name, passport_serial, passport_number) VALUES
('Bobby', 6080, 965400);

DROP TABLE IF EXISTS locking_items CASCADE;
CREATE TABLE locking_items (id bigserial PRIMARY KEY, value int, version int);

DROP TABLE IF EXISTS alive_beans CASCADE;
CREATE TABLE alive_beans (id bigserial PRIMARY KEY, name varchar(255));

DROP TABLE IF EXISTS bottomless_boxes CASCADE;
CREATE TABLE bottomless_boxes (id bigserial PRIMARY KEY, title varchar(255));
INSERT INTO bottomless_boxes (title) VALUES
('Big Green Box'),
('Red Box');

DROP TABLE IF EXISTS things CASCADE;
CREATE TABLE things (id bigserial, title varchar(255), box_id bigint REFERENCES bottomless_boxes (id));
INSERT INTO things (title, box_id) VALUES
('toy', 1),
('ball', 1),
('pen', 2),
('pencil', null);

DROP TABLE IF EXISTS simple_entities CASCADE;
CREATE TABLE simple_entities (id bigserial PRIMARY KEY, name varchar(255));
INSERT INTO simple_entities (name) VALUES ('item 1');

DROP TABLE IF EXISTS orders CASCADE;
CREATE TABLE orders (id bigserial PRIMARY KEY, title varchar(255));
INSERT INTO orders (title) VALUES ('order 1');

DROP TABLE IF EXISTS products CASCADE;
CREATE TABLE products (id bigserial PRIMARY KEY, title varchar(255), price int, order_id bigint references orders(id));
INSERT INTO products (title, price, order_id) VALUES ('product 1', 100, 1);

DROP TABLE IF EXISTS products_test CASCADE;
CREATE TABLE products_test (id bigserial PRIMARY KEY, title varchar(255), price int);
INSERT INTO products_test (title, price) VALUES ('product 1', 100),('product 2', 200),('product 3', 300);

COMMIT;
14 changes: 14 additions & 0 deletions hibernate-app.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="FacetManager">
<facet type="hibernate" name="Hibernate">
<configuration>
<datasource-map>
<unit-entry name="configs:oracle:hibernate.cfg.xml" />
</datasource-map>
<naming-strategy-map />
<deploymentDescriptor name="hibernate.cfg.xml" url="file://$MODULE_DIR$/src/main/resources/configs/oracle/hibernate.cfg.xml" />
</configuration>
</facet>
</component>
</module>
161 changes: 161 additions & 0 deletions hibernate.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<style>
p, li { text-align: justify; }
table, tr, td { border: 1px solid black; }
.blocks td { height: 40px; text-align: center; }
p.reblock { border: 1px solid #ddd; padding: 10px; border-radius: 5px; background-color: #fff; box-shadow: 0 2px 4px rgba(0,0,0,0.25), 0 4px 4px rgba(0,0,0,0.22); }
.tab td { height: 40px; padding: 4px; }




</style>
</head>
<body>
<div class="container">
<h1 class="p-3 mb-2 bg-dark text-white" style="text-align: center;">Hibernate</h1>

<h2>Сущности (Entities)</h2>

<p>An entity is a lightweight persistent domain object. The primary programming artifact is the entity class. An
entity class may make use of auxiliary classes that serve as helper classes or that are used to represent the
state of the entity.</p>

<h3>Entity Class</h3>

<ul>
<li>
The entity class must be annotated with the Entity annotation or denoted in the XML descriptor as an
entity.
</li>
<li>
The entity class must have a no-arg constructor. The entity class may have other constructors as well.
The no-arg constructor must be public or protected.
</li>
<li>
The entity class must be a top-level class. An enum or interface must not be designated as an entity.
</li>
<li>
The entity class must not be final. No methods or persistent instance variables of the entity class may be
final.
</li>
<li>
If an entity instance is to be passed by value as a detached object (e.g., through a remote interface), the
entity class must implement the Serializable interface.
</li>
<li>
Entities support inheritance, polymorphic associations, and polymorphic queries.
</li>
<li>
Both abstract and concrete classes can be entities. Entities may extend non-entity classes as well as
entity classes, and non-entity classes may extend entity classes.
</li>
<li>
The persistent state of an entity is represented by instance variables, which may correspond to Java-
Beans properties. An instance variable must be directly accessed only from within the methods of the
entity by the entity instance itself. Instance variables must not be accessed by clients of the entity. The
state of the entity is available to clients only through the entity’s methods—i.e., accessor methods (getter/
setter methods) or other business methods.
</li>
</ul>

<h3>Entity State in Persistence Context</h3>

<ul>
<li>
<b>transient</b><br>
the entity has just been instantiated and is not associated with a persistence context.
It has no persistent representation in the database and typically no identifier value
has been assigned (unless the assigned generator was used).
</li>
<li>
<b>managed, or persistent</b><br>
the entity has an associated identifier and is associated with a persistence
context. It may or may not physically exist in the database yet.
</li>
<li>
<b>detached</b><br>
the entity has an associated identifier but is no longer associated with a persistence
context (usually because the persistence context was closed or the instance was evicted
from the context)
</li>
<li>
<b>removed</b><br>
the entity has an associated identifier and is associated with a persistence context,
however, it is scheduled for removal from the database.
</li>
</ul>

<h3>Entity Lifecycle</h3>
<img src="lifecycle.jpg">
<ul>
<li>
<b>Entity Instance Creation</b><br>
Entity instances are created by means of the new operation. An entity instance, when first created by
new is not yet persistent. An instance becomes persistent by means of the EntityManager API.
</li><br>
<li>
<b>Persisting an Entity Instance</b><br>
A new entity instance becomes both managed and persistent by invoking the persist method on it or
by cascading the persist operation.
The semantics of the persist operation, applied to an entity X are as follows:
<ul>
<li>
If X is a new entity, it becomes managed. The entity X will be entered into the database at or
before transaction commit or as a result of the flush operation.
</li>
<li>
If X is a preexisting managed entity, it is ignored by the persist operation. However, the persist
operation is cascaded to entities referenced by X, if the relationships from X to these other
entities are annotated with the cascade=PERSIST or cascade=ALL annotation element
value or specified with the equivalent XML descriptor element.
</li>
<li>
If X is a removed entity, it becomes managed.
</li>
<li>
If X is a detached object, the EntityExistsException may be thrown when the persist
operation is invoked, or the EntityExistsException or another PersistenceException
may be thrown at flush or commit time.
</li>
<li>
For all entities Y referenced by a relationship from X, if the relationship to Y has been annotated
with the cascade element value cascade=PERSIST or cascade=ALL, the persist
operation is applied to Y.
</li>
</ul>
</li><br>
<li>
<b>Removal</b><br>
A managed entity instance becomes removed by invoking the remove method on it or by cascading the
remove operation.
The semantics of the remove operation, applied to an entity X are as follows:
<ul>
<li>
If X is a new entity, it is ignored by the remove operation. However, the remove operation is
cascaded to entities referenced by X, if the relationship from X to these other entities is annotated
with the cascade=REMOVE or cascade=ALL annotation element value.
</li>
<li>
If X is a detached entity, an IllegalArgumentException will be thrown by the remove
operation (or the transaction commit will fail).
</li>
<li>
If X is a removed entity, it is ignored by the remove operation.
</li>
<li>
A removed entity X will be removed from the database at or before transaction commit or as a
result of the flush operation.
</li>
</ul>
After an entity has been removed, its state (except for generated state) will be that of the entity at the
point at which the remove operation was called.
</li>
</ul>
</div>
</body>
</html>
Binary file added lifecycle.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading