{"id":18738216,"url":"https://github.com/enginooby-practice/hibernate","last_synced_at":"2026-04-22T16:37:26.260Z","repository":{"id":216465923,"uuid":"275740945","full_name":"enginooby-practice/hibernate","owner":"enginooby-practice","description":"Configured by XML.","archived":false,"fork":false,"pushed_at":"2020-07-19T22:08:57.000Z","size":14998,"stargazers_count":1,"open_issues_count":5,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-27T22:03:02.751Z","etag":null,"topics":["hibernate","hql","java","jpa","mysql","xml"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/enginooby-practice.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-06-29T05:42:02.000Z","updated_at":"2020-07-30T11:16:16.000Z","dependencies_parsed_at":"2024-01-10T12:05:53.230Z","dependency_job_id":null,"html_url":"https://github.com/enginooby-practice/hibernate","commit_stats":null,"previous_names":["enginooby-practice/hibernate"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/enginooby-practice/hibernate","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enginooby-practice%2Fhibernate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enginooby-practice%2Fhibernate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enginooby-practice%2Fhibernate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enginooby-practice%2Fhibernate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/enginooby-practice","download_url":"https://codeload.github.com/enginooby-practice/hibernate/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enginooby-practice%2Fhibernate/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32145870,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-22T15:33:03.595Z","status":"ssl_error","status_checked_at":"2026-04-22T15:30:42.712Z","response_time":58,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hibernate","hql","java","jpa","mysql","xml"],"created_at":"2024-11-07T15:28:42.842Z","updated_at":"2026-04-22T16:37:21.242Z","avatar_url":"https://github.com/enginooby-practice.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Practice topics\n### Configuration\n- Add dependencies: Hibernate and MySQL connector\n- Create database \n[[create-db.sql](https://github.com/cpulover-practice/hibernate/blob/master/sql/create-db.sql)]\n- Configure Hibernate by XML \n[[hibernate.cfg.xml](https://github.com/cpulover-practice/hibernate/blob/master/src/hibernate.cfg.xml)]\n\n### Concepts\n- CRUD features\n- SessionFactory, Session and Transaction\n- Uni-directional and bi-directional relationship\n- Cascade types: CascadeType.ALL, CascadeType.PERSIST (save), CascadeType.REMOVE, CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH\n  - 📌 Use persist() with CascadeType.ALL/PERSIST to save all associated objects.\n- Eager loading and Lazy loading; Fetch types: Fetch.LAZY (prefer), Fetch.EAGER\n  - To retrieve lazy data, a Hibernate session need to be open (connect to the database).\n  - 📌 Resolve Lazy loading issue (because of loading after closing session)\n    - Option 1: Call getter method while session is still open \n  [[EagerLazyDemo](https://github.com/cpulover-practice/hibernate/blob/master/src/com/cpulover/hibernate/onetomany/EagerLazyDemo.java)]\n    - Option 2: Use Hibernate query with HQL [hibernate.query.Query]\n  [[FetchJoinDemo](https://github.com/cpulover-practice/hibernate/blob/master/src/com/cpulover/hibernate/onetomany/FetchJoinDemo.java)]\n\n\n### Entity Class\n- Add no-args constructor (required by Hibernate)\n- 📌 Add convenience methods for bi-directional one/many-to-many relationship \n[[Instructor](https://github.com/cpulover-practice/hibernate/blob/master/src/com/cpulover/hibernate/entity/Instructor.java)]\n- *__@Entity__* [javax.persistence]\n- *__@Id__* and *__@GeneratedValue__* (prefer GenerationType.IDENTITY)\n- *__@Table:__* map to table in the database [javax.persistence]\n- *__@Column:__* map to column of the table\n- *__@OneToOne__* \n[[Instructor](https://github.com/cpulover-practice/hibernate/blob/master/src/com/cpulover/hibernate/entity/Instructor.java)\n/[InstructorDetail](https://github.com/cpulover-practice/hibernate/blob/master/src/com/cpulover/hibernate/entity/InstructorDetail.java)]\n  - Uni-directional (use *__@JoinColumn__*) and bi-directional (add *`mappedBy`* to *__@OneToOne__*)\n  -  *__@JoinColum:__* used in the entity class containing the foreign key.\n  - *`mappedBy`* attribute: used in the class entity which does not contain the foreign key.\n- *__@OneToMany__* \n[[Course](https://github.com/cpulover-practice/hibernate/blob/master/src/com/cpulover/hibernate/entity/Course.java)\n/[Review](https://github.com/cpulover-practice/hibernate/blob/master/src/com/cpulover/hibernate/entity/Review.java)]\n  - Uni-directional: *__@JoinColumn__* used in the class entity which does not contain the foreign key.\n- *__@ManyToMany__* \n[[Student](https://github.com/cpulover-practice/hibernate/blob/master/src/com/cpulover/hibernate/entity/Student.java)\n/[Course](https://github.com/cpulover-practice/hibernate/blob/master/src/com/cpulover/hibernate/entity/Course.java)]\n  - *`fetch`* should be FetchType.LAZY\n  - *`cascade`* should omit CascadeType.REMOVE\n  - *__@JoinTable__* with *`joinColumns`* and *`inverseJoinColumns`* for each foreign key\n\n### App Flow\n[[CreateDemo](https://github.com/cpulover-practice/hibernate/blob/master/src/com/cpulover/hibernate/onetoone/CreateDemo.java)]\n1. Create a Session Factory including all the entities\n2. Create a Session\n3. Start a Transaction\n4. Perfrom CRUD (save/permit if create or update entities)\n5. Commit the Transaction\n6. Handle connection leak issue: catch error and close Session.\n7. Close the Factory\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenginooby-practice%2Fhibernate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fenginooby-practice%2Fhibernate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenginooby-practice%2Fhibernate/lists"}