├── .gitattributes ├── .gitignore ├── README.md └── Spring-OODD ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── oodd │ │ └── spring │ │ ├── classtableinheritance │ │ ├── controller │ │ │ └── EmployeeController.java │ │ ├── dao │ │ │ ├── EmployeeDao.java │ │ │ └── impl │ │ │ │ └── EmployeeDaoImpl.java │ │ ├── dto │ │ │ ├── ContractorEmployeeDto.java │ │ │ ├── EmployeeDto.java │ │ │ └── PermanentEmployeeDto.java │ │ ├── entity │ │ │ ├── ContractorEmployee.java │ │ │ ├── Employee.java │ │ │ └── PermanentEmployee.java │ │ ├── mapper │ │ │ └── EmployeeMapper.java │ │ ├── mock │ │ │ ├── EmployeeInMemoryDB.java │ │ │ └── EmployeeMockController.java │ │ └── service │ │ │ ├── EmployeeService.java │ │ │ └── impl │ │ │ └── EmployeeServiceImpl.java │ │ ├── concretetableinheritance │ │ ├── controller │ │ │ └── EstateController.java │ │ ├── dao │ │ │ ├── EstateDao.java │ │ │ └── impl │ │ │ │ └── EstateDaoImpl.java │ │ ├── dto │ │ │ ├── BuildingDto.java │ │ │ ├── EstateDto.java │ │ │ └── LandDto.java │ │ ├── entity │ │ │ ├── Building.java │ │ │ ├── Estate.java │ │ │ └── Land.java │ │ ├── mapper │ │ │ └── EstateMapper.java │ │ ├── mock │ │ │ ├── EstateInMemoryDB.java │ │ │ └── EstateMockController.java │ │ └── service │ │ │ ├── EstateService.java │ │ │ └── impl │ │ │ └── EstateServiceImpl.java │ │ ├── manytomanybidirectional │ │ ├── controller │ │ │ ├── AccountController.java │ │ │ ├── ClientAccountController.java │ │ │ └── ClientController.java │ │ ├── dao │ │ │ ├── AccountDao.java │ │ │ ├── ClientAccountDao.java │ │ │ ├── ClientDao.java │ │ │ └── impl │ │ │ │ ├── AccountDaoImpl.java │ │ │ │ ├── ClientAccountDaoImpl.java │ │ │ │ └── ClientDaoImpl.java │ │ ├── dto │ │ │ ├── AccountDto.java │ │ │ ├── ClientAccountDto.java │ │ │ └── ClientDto.java │ │ ├── entity │ │ │ ├── Account.java │ │ │ └── Client.java │ │ ├── mapper │ │ │ ├── AccountMapper.java │ │ │ └── ClientMapper.java │ │ ├── mock │ │ │ ├── AccountInMemoryDB.java │ │ │ ├── AccountMockController.java │ │ │ ├── ClientAccountInMemoryDB.java │ │ │ ├── ClientAccountMockController.java │ │ │ ├── ClientInMemoryDB.java │ │ │ └── ClientMockController.java │ │ └── service │ │ │ ├── AccountService.java │ │ │ ├── ClientAccountService.java │ │ │ ├── ClientService.java │ │ │ └── impl │ │ │ ├── AccountServiceImpl.java │ │ │ ├── ClientAccountServiceImpl.java │ │ │ └── ClientServiceImpl.java │ │ ├── manytomanybidirectionalwithjoinattribute │ │ ├── controller │ │ │ ├── AuthorController.java │ │ │ ├── ManuscriptAuthorController.java │ │ │ └── ManuscriptController.java │ │ ├── dao │ │ │ ├── AuthorDao.java │ │ │ ├── ManuscriptAuthorDao.java │ │ │ ├── ManuscriptDao.java │ │ │ └── impl │ │ │ │ ├── AuthorDaoImpl.java │ │ │ │ ├── ManuscriptAuthorDaoImpl.java │ │ │ │ └── ManuscriptDaoImpl.java │ │ ├── dto │ │ │ ├── AuthorDto.java │ │ │ ├── ManuscriptAuthorDto.java │ │ │ └── ManuscriptDto.java │ │ ├── entity │ │ │ ├── Author.java │ │ │ ├── Manuscript.java │ │ │ ├── ManuscriptAuthor.java │ │ │ └── ManuscriptAuthorId.java │ │ ├── mapper │ │ │ ├── AuthorMapper.java │ │ │ └── ManuscriptMapper.java │ │ ├── mock │ │ │ ├── AuthorInMemoryDB.java │ │ │ ├── AuthorMockController.java │ │ │ ├── ManuscriptAuthorInMemoryDB.java │ │ │ ├── ManuscriptAuthorMockController.java │ │ │ ├── ManuscriptInMemoryDB.java │ │ │ └── ManuscriptMockController.java │ │ └── service │ │ │ ├── AuthorService.java │ │ │ ├── ManuscriptAuthorService.java │ │ │ ├── ManuscriptService.java │ │ │ └── impl │ │ │ ├── AuthorServiceImpl.java │ │ │ ├── ManuscriptAuthorServiceImpl.java │ │ │ └── ManuscriptServiceImpl.java │ │ ├── manytomanyselfreference │ │ ├── controller │ │ │ ├── MemberController.java │ │ │ └── MemberMemberController.java │ │ ├── dao │ │ │ ├── MemberDao.java │ │ │ ├── MemberMemberDao.java │ │ │ └── impl │ │ │ │ ├── MemberDaoImpl.java │ │ │ │ └── MemberMemberDaoImpl.java │ │ ├── dto │ │ │ ├── MemberDto.java │ │ │ └── MemberMemberDto.java │ │ ├── entity │ │ │ └── Member.java │ │ ├── mapper │ │ │ └── MemberMapper.java │ │ ├── mock │ │ │ ├── MemberInMemoryDB.java │ │ │ ├── MemberMemberInMemoryDB.java │ │ │ ├── MemberMemberMockController.java │ │ │ └── MemberMockController.java │ │ └── service │ │ │ ├── MemberMemberService.java │ │ │ ├── MemberService.java │ │ │ └── impl │ │ │ ├── MemberMemberServiceImpl.java │ │ │ └── MemberServiceImpl.java │ │ ├── manytomanyselfreferencewithjoinattribute │ │ ├── controller │ │ │ ├── WorkerController.java │ │ │ └── WorkerWorkerController.java │ │ ├── dao │ │ │ ├── WorkerDao.java │ │ │ ├── WorkerWorkerDao.java │ │ │ └── impl │ │ │ │ ├── WorkerDaoImpl.java │ │ │ │ └── WorkerWorkerDaoImpl.java │ │ ├── dto │ │ │ ├── WorkerDto.java │ │ │ └── WorkerWorkerDto.java │ │ ├── entity │ │ │ ├── Worker.java │ │ │ ├── WorkerWorker.java │ │ │ └── WorkerWorkerId.java │ │ ├── mapper │ │ │ └── WorkerMapper.java │ │ ├── mock │ │ │ ├── WorkerInMemoryDB.java │ │ │ ├── WorkerMockController.java │ │ │ ├── WorkerWorkerInMemoryDB.java │ │ │ └── WorkerWorkerMockController.java │ │ └── service │ │ │ ├── WorkerService.java │ │ │ ├── WorkerWorkerService.java │ │ │ └── impl │ │ │ ├── WorkerServiceImpl.java │ │ │ └── WorkerWorkerServiceImpl.java │ │ ├── manytomanyunidirectional │ │ ├── controller │ │ │ ├── GroupController.java │ │ │ ├── UserController.java │ │ │ └── UserGroupController.java │ │ ├── dao │ │ │ ├── GroupDao.java │ │ │ ├── UserDao.java │ │ │ ├── UserGroupDao.java │ │ │ └── impl │ │ │ │ ├── GroupDaoImpl.java │ │ │ │ ├── UserDaoImpl.java │ │ │ │ └── UserGroupDaoImpl.java │ │ ├── dto │ │ │ ├── GroupDto.java │ │ │ ├── UserDto.java │ │ │ └── UserGroupDto.java │ │ ├── entity │ │ │ ├── Group.java │ │ │ └── User.java │ │ ├── mapper │ │ │ ├── GroupMapper.java │ │ │ └── UserMapper.java │ │ ├── mock │ │ │ ├── GroupInMemoryDB.java │ │ │ ├── GroupMockController.java │ │ │ ├── UserGroupInMemoryDB.java │ │ │ ├── UserGroupMockController.java │ │ │ ├── UserInMemoryDB.java │ │ │ └── UserMockController.java │ │ └── service │ │ │ ├── GroupService.java │ │ │ ├── UserGroupService.java │ │ │ ├── UserService.java │ │ │ └── impl │ │ │ ├── GroupServiceImpl.java │ │ │ ├── UserGroupServiceImpl.java │ │ │ └── UserServiceImpl.java │ │ ├── onetomanybidirectional │ │ ├── controller │ │ │ └── ItemController.java │ │ ├── dao │ │ │ ├── ItemDao.java │ │ │ └── impl │ │ │ │ └── ItemDaoImpl.java │ │ ├── dto │ │ │ └── ItemDto.java │ │ ├── entity │ │ │ ├── Feature.java │ │ │ └── Item.java │ │ ├── mapper │ │ │ └── ItemMapper.java │ │ ├── mock │ │ │ ├── ItemInMemoryDB.java │ │ │ └── ItemMockController.java │ │ └── service │ │ │ ├── ItemService.java │ │ │ └── impl │ │ │ └── ItemServiceImpl.java │ │ ├── onetomanyselfreference │ │ ├── controller │ │ │ └── CategoryController.java │ │ ├── dao │ │ │ ├── CategoryDao.java │ │ │ └── impl │ │ │ │ └── CategoryDaoImpl.java │ │ ├── dto │ │ │ └── CategoryDto.java │ │ ├── entity │ │ │ └── Category.java │ │ ├── mapper │ │ │ └── CategoryMapper.java │ │ ├── mock │ │ │ ├── CategoryInMemoryDB.java │ │ │ └── CategoryMockController.java │ │ └── service │ │ │ ├── CategoryService.java │ │ │ └── impl │ │ │ └── CategoryServiceImpl.java │ │ ├── onetomanyunidirectional │ │ ├── controller │ │ │ └── PersonController.java │ │ ├── dao │ │ │ ├── PersonDao.java │ │ │ └── impl │ │ │ │ └── PersonDaoImpl.java │ │ ├── dto │ │ │ └── PersonDto.java │ │ ├── entity │ │ │ ├── Person.java │ │ │ └── Phone.java │ │ ├── mapper │ │ │ └── PersonMapper.java │ │ ├── mock │ │ │ ├── PersonInMemoryDB.java │ │ │ └── PersonMockController.java │ │ └── service │ │ │ ├── PersonService.java │ │ │ └── impl │ │ │ └── PersonServiceImpl.java │ │ ├── onetoonebidirectional │ │ ├── controller │ │ │ └── CustomerController.java │ │ ├── dao │ │ │ ├── CustomerDao.java │ │ │ └── impl │ │ │ │ └── CustomerDaoImpl.java │ │ ├── dto │ │ │ └── CustomerDto.java │ │ ├── entity │ │ │ ├── Cart.java │ │ │ └── Customer.java │ │ ├── mapper │ │ │ └── CustomerMapper.java │ │ ├── mock │ │ │ ├── CustomerInMemoryDB.java │ │ │ └── CustomerMockController.java │ │ └── service │ │ │ ├── CustomerService.java │ │ │ └── impl │ │ │ └── CustomerServiceImpl.java │ │ ├── onetooneselfreference │ │ ├── controller │ │ │ └── StudentController.java │ │ ├── dao │ │ │ ├── StudentDao.java │ │ │ └── impl │ │ │ │ └── StudentDaoImpl.java │ │ ├── dto │ │ │ └── StudentDto.java │ │ ├── entity │ │ │ └── Student.java │ │ ├── mapper │ │ │ └── StudentMapper.java │ │ ├── mock │ │ │ ├── StudentInMemoryDB.java │ │ │ └── StudentMockController.java │ │ └── service │ │ │ ├── StudentService.java │ │ │ └── impl │ │ │ └── StudentServiceImpl.java │ │ ├── onetooneunidirectional │ │ ├── controller │ │ │ └── BookController.java │ │ ├── dao │ │ │ ├── BookDao.java │ │ │ └── impl │ │ │ │ └── BookDaoImpl.java │ │ ├── dto │ │ │ └── BookDto.java │ │ ├── entity │ │ │ ├── Book.java │ │ │ └── Shipping.java │ │ ├── mapper │ │ │ └── BookMapper.java │ │ ├── mock │ │ │ ├── BookInMemoryDB.java │ │ │ └── BookMockController.java │ │ └── service │ │ │ ├── BookService.java │ │ │ └── impl │ │ │ └── BookServiceImpl.java │ │ ├── singletableinheritance │ │ ├── controller │ │ │ └── ProtocolController.java │ │ ├── dao │ │ │ ├── ProtocolDao.java │ │ │ └── impl │ │ │ │ └── ProtocolDaoImpl.java │ │ ├── dto │ │ │ ├── ProtocolDto.java │ │ │ ├── SNMPDto.java │ │ │ └── TCPDto.java │ │ ├── entity │ │ │ ├── Protocol.java │ │ │ ├── SNMP.java │ │ │ └── TCP.java │ │ ├── mapper │ │ │ └── ProtocolMapper.java │ │ ├── mock │ │ │ ├── ProtocolInMemoryDB.java │ │ │ └── ProtocolMockController.java │ │ └── service │ │ │ ├── ProtocolService.java │ │ │ └── impl │ │ │ └── ProtocolServiceImpl.java │ │ └── standalone │ │ ├── controller │ │ └── ProductController.java │ │ ├── dao │ │ ├── ProductDao.java │ │ └── impl │ │ │ └── ProductDaoImpl.java │ │ ├── dto │ │ └── ProductDto.java │ │ ├── entity │ │ └── Product.java │ │ ├── mapper │ │ └── ProductMapper.java │ │ ├── mock │ │ ├── ProductInMemoryDB.java │ │ └── ProductMockController.java │ │ └── service │ │ ├── ProductService.java │ │ └── impl │ │ └── ProductServiceImpl.java ├── resources │ ├── datasource.properties │ └── hibernate.cfg.xml └── webapp │ ├── WEB-INF │ ├── spring-servlet.xml │ └── web.xml │ ├── js │ ├── classtableinheritance │ │ ├── employee.js │ │ └── employeemock.js │ ├── concretetableinheritance │ │ ├── estate.js │ │ └── estatemock.js │ ├── jquery │ │ └── jquery-1.7.1.min.js │ ├── manytomanybidirectional │ │ ├── clientaccount.js │ │ └── clientaccountmock.js │ ├── manytomanybidirectionalwithjoinattribute │ │ ├── manuscriptauthor.js │ │ └── manuscriptauthormock.js │ ├── manytomanyselfreference │ │ ├── membermember.js │ │ └── membermembermock.js │ ├── manytomanyselfreferencewithjoinattribute │ │ ├── workerworker.js │ │ └── workerworkermock.js │ ├── manytomanyunidirectional │ │ ├── usergroup.js │ │ └── usergroupmock.js │ ├── onetomanybidirectional │ │ ├── item.js │ │ └── itemmock.js │ ├── onetomanyselfreference │ │ ├── category.js │ │ └── categorymock.js │ ├── onetomanyunidirectional │ │ ├── person.js │ │ └── personmock.js │ ├── onetoonebidirectional │ │ ├── customer.js │ │ └── customermock.js │ ├── onetooneselfreference │ │ ├── student.js │ │ └── studentmock.js │ ├── onetooneunidirectional │ │ ├── book.js │ │ └── bookmock.js │ ├── singletableinheritance │ │ ├── protocol.js │ │ └── protocolmock.js │ └── standalone │ │ ├── product.js │ │ └── productmock.js │ └── jsp │ ├── classtableinheritance │ ├── employee.jsp │ └── employeemock.jsp │ ├── concretetableinheritance │ ├── estate.jsp │ └── estatemock.jsp │ ├── header.jsp │ ├── main.jsp │ ├── manytomanybidirectional │ ├── clientaccount.jsp │ └── clientaccountmock.jsp │ ├── manytomanybidirectionalwithjoinattribute │ ├── manuscriptauthor.jsp │ └── manuscriptauthormock.jsp │ ├── manytomanyselfreference │ ├── membermember.jsp │ └── membermembermock.jsp │ ├── manytomanyselfreferencewithjoinattribute │ ├── workerworker.jsp │ └── workerworkermock.jsp │ ├── manytomanyunidirectional │ ├── usergroup.jsp │ └── usergroupmock.jsp │ ├── onetomanybidirectional │ ├── item.jsp │ └── itemmock.jsp │ ├── onetomanyselfreference │ ├── category.jsp │ └── categorymock.jsp │ ├── onetomanyunidirectional │ ├── person.jsp │ └── personmock.jsp │ ├── onetoonebidirectional │ ├── customer.jsp │ └── customermock.jsp │ ├── onetooneselfreference │ ├── student.jsp │ └── studentmock.jsp │ ├── onetooneunidirectional │ ├── book.jsp │ └── bookmock.jsp │ ├── singletableinheritance │ ├── protocol.jsp │ └── protocolmock.jsp │ ├── standalone │ ├── product.jsp │ └── productmock.jsp │ └── topics.jsp └── test ├── java └── com │ └── oodd │ └── spring │ ├── classtableinheritance │ ├── controller │ │ └── EmployeeControllerTest.java │ ├── dao │ │ └── EmployeeDaoImplTest.java │ ├── entity │ │ ├── ContractorEmployeeTest.java │ │ ├── EmployeeTest.java │ │ └── PermanentEmployeeTest.java │ └── service │ │ └── EmployeeServiceImplTest.java │ ├── concretetableinheritance │ ├── controller │ │ └── EstateControllerTest.java │ ├── dao │ │ └── EstateDaoImplTest.java │ ├── entity │ │ ├── BuildingTest.java │ │ ├── EstateTest.java │ │ └── LandTest.java │ └── service │ │ └── EstateServiceImplTest.java │ ├── manytomanybidirectional │ ├── controller │ │ ├── AccountControllerTest.java │ │ ├── ClientAccountControllerTest.java │ │ └── ClientControllerTest.java │ ├── dao │ │ ├── AccountDaoImplTest.java │ │ ├── ClientAccountDaoImplTest.java │ │ └── ClientDaoImplTest.java │ ├── entity │ │ ├── AccountTest.java │ │ └── ClientTest.java │ └── service │ │ ├── AccountServiceImplTest.java │ │ ├── ClientAccountServiceImplTest.java │ │ └── ClientServiceImplTest.java │ ├── manytomanybidirectionalwithjoinattribute │ ├── controller │ │ ├── AuthorControllerTest.java │ │ ├── ManuscriptAuthorControllerTest.java │ │ └── ManuscriptControllerTest.java │ ├── dao │ │ ├── AuthorDaoImplTest.java │ │ ├── ManuscriptAuthorDaoImplTest.java │ │ └── ManuscriptDaoImplTest.java │ ├── entity │ │ ├── AuthorTest.java │ │ ├── ManuscriptAuthorTest.java │ │ └── ManuscriptTest.java │ └── service │ │ ├── AuthorServiceImplTest.java │ │ ├── ManuscriptAuthorServiceImplTest.java │ │ └── ManuscriptServiceImplTest.java │ ├── manytomanyselfreference │ ├── controller │ │ ├── MemberControllerTest.java │ │ └── MemberMemberControllerTest.java │ ├── dao │ │ ├── MemberDaoImplTest.java │ │ └── MemberMemberDaoImplTest.java │ ├── entity │ │ └── MemberTest.java │ └── service │ │ ├── MemberMemberServiceImplTest.java │ │ └── MemberServiceImplTest.java │ ├── manytomanyselfreferencewithjoinattribute │ ├── controller │ │ ├── WorkerControllerTest.java │ │ └── WorkerWorkerControllerTest.java │ ├── dao │ │ ├── WorkerDaoImplTest.java │ │ └── WorkerWorkerDaoImplTest.java │ ├── entity │ │ ├── WorkerTest.java │ │ └── WorkerWorkerTest.java │ └── service │ │ ├── WorkerServiceImplTest.java │ │ └── WorkerWorkerServiceImplTest.java │ ├── manytomanyunidirectional │ ├── controller │ │ ├── GroupControllerTest.java │ │ ├── UserControllerTest.java │ │ └── UserGroupControllerTest.java │ ├── dao │ │ ├── GroupDaoImplTest.java │ │ ├── UserDaoImplTest.java │ │ └── UserGroupDaoImplTest.java │ ├── entity │ │ ├── GroupTest.java │ │ └── UserTest.java │ └── service │ │ ├── GroupServiceImplTest.java │ │ ├── UserGroupServiceImplTest.java │ │ └── UserServiceImplTest.java │ ├── onetomanybidirectional │ ├── controller │ │ └── ItemControllerTest.java │ ├── dao │ │ └── ItemDaoImplTest.java │ ├── entity │ │ └── ItemTest.java │ └── service │ │ └── ItemServiceImplTest.java │ ├── onetomanyselfreference │ ├── controller │ │ └── CategoryControllerTest.java │ ├── dao │ │ └── CategoryDaoImplTest.java │ ├── entity │ │ └── CategoryTest.java │ └── service │ │ └── CategoryServiceImplTest.java │ ├── onetomanyunidirectional │ ├── controller │ │ └── PersonControllerTest.java │ ├── dao │ │ └── PersonDaoImplTest.java │ ├── entity │ │ └── PersonTest.java │ └── service │ │ └── PersonServiceImplTest.java │ ├── onetoonebidirectional │ ├── controller │ │ └── CustomerControllerTest.java │ ├── dao │ │ └── CustomerDaoImplTest.java │ ├── entity │ │ └── CustomerTest.java │ └── service │ │ └── CustomerServiceImplTest.java │ ├── onetooneselfreference │ ├── controller │ │ └── StudentControllerTest.java │ ├── dao │ │ └── StudentDaoImplTest.java │ ├── entity │ │ └── StudentTest.java │ └── service │ │ └── StudentServiceImplTest.java │ ├── onetooneunidirectional │ ├── controller │ │ └── BookControllerTest.java │ ├── dao │ │ └── BookDaoImplTest.java │ ├── entity │ │ └── BookTest.java │ └── service │ │ └── BookServiceImplTest.java │ ├── singletableinheritance │ ├── controller │ │ └── ProtocolControllerTest.java │ ├── dao │ │ └── ProtocolDaoImplTest.java │ ├── entity │ │ ├── ProtocolTest.java │ │ ├── SNMPTest.java │ │ └── TCPTest.java │ └── service │ │ └── ProtocolServiceImplTest.java │ └── standalone │ ├── controller │ └── ProductControllerTest.java │ ├── dao │ └── ProductDaoImplTest.java │ ├── entity │ └── ProductTest.java │ └── service │ └── ProductServiceImplTest.java └── resources ├── context.xml ├── datasource.properties └── hibernate.cfg.xml /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # ========================= 18 | # Operating System Files 19 | # ========================= 20 | 21 | # OSX 22 | # ========================= 23 | 24 | .DS_Store 25 | .AppleDouble 26 | .LSOverride 27 | 28 | # Icon must end with two \r 29 | Icon 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear on external disk 35 | .Spotlight-V100 36 | .Trashes 37 | 38 | # Directories potentially created on remote AFP share 39 | .AppleDB 40 | .AppleDesktop 41 | Network Trash Folder 42 | Temporary Items 43 | .apdisk 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Spring Hibernate TDD REST Agile Java Book 2 | ========================================= 3 | The repository is the sample code of a Spring and Hibernate book. The author and his team has put lot of effort in putting together this vast useful code. If you find the information useful, do buy the book. 4 | 5 | Title of the book: Spring, Hibernate, Data Modeling, REST and TDD: Agile Java Design and Development 6 | 7 | Blog: http://amritendude.blogspot.in/2014/06/new-book-on-spring-4-and-hibernate-4.html 8 | 9 | Availability: Available on all Amazon sites 10 | 11 | Amritendu De 12 | (amritendu_de@yahoo.com) 13 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/classtableinheritance/dao/EmployeeDao.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.classtableinheritance.dao; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.classtableinheritance.entity.Employee; 6 | 7 | public interface EmployeeDao { 8 | public void insert(Employee employee) ; 9 | public List getAll(); 10 | public Employee getById(Integer id); 11 | public void delete(Employee employee) ; 12 | public void update(Employee employee); 13 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/classtableinheritance/dao/impl/EmployeeDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.classtableinheritance.dao.impl; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Repository; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import com.oodd.spring.classtableinheritance.dao.EmployeeDao; 11 | import com.oodd.spring.classtableinheritance.entity.Employee; 12 | 13 | @Repository 14 | @Transactional 15 | public class EmployeeDaoImpl implements EmployeeDao { 16 | @Autowired 17 | private SessionFactory sessionFactory ; 18 | @Override 19 | public void insert(Employee employee) { 20 | sessionFactory.getCurrentSession().save(employee); 21 | } 22 | @SuppressWarnings("unchecked") 23 | @Override 24 | public List getAll() { 25 | return sessionFactory.getCurrentSession().createQuery("select employee from Employee employee order by employee.id desc").list(); 26 | } 27 | @Override 28 | public Employee getById(Integer id) { 29 | return (Employee) sessionFactory.getCurrentSession().get(Employee.class, id); 30 | } 31 | @Override 32 | public void delete(Employee employee) { 33 | sessionFactory.getCurrentSession().delete(employee); 34 | } 35 | @Override 36 | public void update(Employee employee) { 37 | sessionFactory.getCurrentSession().merge(employee); 38 | } 39 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/classtableinheritance/dto/ContractorEmployeeDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.classtableinheritance.dto; 2 | 3 | import com.fasterxml.jackson.annotation.JsonProperty; 4 | 5 | 6 | public class ContractorEmployeeDto extends EmployeeDto { 7 | 8 | @JsonProperty("hourlyRate") 9 | private Integer hourlyRate; 10 | @JsonProperty("overtimeRate") 11 | private Integer overtimeRate; 12 | 13 | public Integer getHourlyRate() { 14 | return hourlyRate; 15 | } 16 | public void setHourlyRate(Integer hourlyRate) { 17 | this.hourlyRate = hourlyRate; 18 | } 19 | public Integer getOvertimeRate() { 20 | return overtimeRate; 21 | } 22 | public void setOvertimeRate(Integer overtimeRate) { 23 | this.overtimeRate = overtimeRate; 24 | } 25 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/classtableinheritance/dto/EmployeeDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.classtableinheritance.dto; 2 | 3 | import com.fasterxml.jackson.annotation.JsonSubTypes; 4 | import com.fasterxml.jackson.annotation.JsonSubTypes.Type; 5 | import com.fasterxml.jackson.annotation.JsonTypeInfo; 6 | 7 | @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type") 8 | @JsonSubTypes({ 9 | @Type(value = PermanentEmployeeDto.class, name = "permanentEmployee"), 10 | @Type(value = ContractorEmployeeDto.class, name = "contractorEmployee") }) 11 | public class EmployeeDto { 12 | 13 | private Integer id; 14 | private String name; 15 | 16 | public Integer getId() { 17 | return id; 18 | } 19 | public void setId(Integer id) { 20 | this.id = id; 21 | } 22 | public String getName() { 23 | return name; 24 | } 25 | public void setName(String name) { 26 | this.name = name; 27 | } 28 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/classtableinheritance/dto/PermanentEmployeeDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.classtableinheritance.dto; 2 | 3 | import com.fasterxml.jackson.annotation.JsonProperty; 4 | 5 | 6 | public class PermanentEmployeeDto extends EmployeeDto { 7 | 8 | @JsonProperty("leaves") 9 | private Integer leaves; 10 | @JsonProperty("salary") 11 | private Integer salary; 12 | 13 | public Integer getLeaves() { 14 | return leaves; 15 | } 16 | public void setLeaves(Integer leaves) { 17 | this.leaves = leaves; 18 | } 19 | public Integer getSalary() { 20 | return salary; 21 | } 22 | public void setSalary(Integer salary) { 23 | this.salary = salary; 24 | } 25 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/classtableinheritance/entity/ContractorEmployee.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.classtableinheritance.entity; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.PrimaryKeyJoinColumn; 6 | import javax.persistence.Table; 7 | 8 | @Entity 9 | @Table(name="CONTRACTOR_EMPLOYEE") 10 | @PrimaryKeyJoinColumn(name="ID") 11 | public class ContractorEmployee extends Employee { 12 | 13 | private Integer hourlyRate; 14 | private Integer overtimeRate; 15 | 16 | @Column(name = "HOURLEY_RATE", nullable = false, length = 100) 17 | public Integer getHourlyRate() { 18 | return hourlyRate; 19 | } 20 | public void setHourlyRate(Integer hourlyRate) { 21 | this.hourlyRate = hourlyRate; 22 | } 23 | 24 | @Column(name = "OVERTIME_RATE", nullable = false, length = 100) 25 | public Integer getOvertimeRate() { 26 | return overtimeRate; 27 | } 28 | public void setOvertimeRate(Integer overtimeRate) { 29 | this.overtimeRate = overtimeRate; 30 | } 31 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/classtableinheritance/entity/Employee.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.classtableinheritance.entity; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Id; 8 | import javax.persistence.Inheritance; 9 | import javax.persistence.InheritanceType; 10 | import javax.persistence.Table; 11 | 12 | @Entity 13 | @Table(name = "EMPLOYEE") 14 | @Inheritance(strategy = InheritanceType.JOINED) 15 | public class Employee { 16 | 17 | private Integer id; 18 | private String name; 19 | 20 | @Id 21 | @GeneratedValue(strategy = GenerationType.TABLE) 22 | @Column(name = "ID") 23 | public Integer getId() { 24 | return id; 25 | } 26 | 27 | public void setId(Integer id) { 28 | this.id = id; 29 | } 30 | 31 | @Column(name = "NAME", nullable = false, length = 100) 32 | public String getName() { 33 | return name; 34 | } 35 | 36 | public void setName(String name) { 37 | this.name = name; 38 | } 39 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/classtableinheritance/entity/PermanentEmployee.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.classtableinheritance.entity; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.PrimaryKeyJoinColumn; 6 | import javax.persistence.Table; 7 | 8 | @Entity 9 | @Table(name="PERMANENT_EMPLOYEE") 10 | @PrimaryKeyJoinColumn(name="ID") 11 | public class PermanentEmployee extends Employee { 12 | 13 | private Integer leaves; 14 | private Integer salary; 15 | 16 | @Column(name = "LEAVES", nullable = false, length = 100) 17 | public Integer getLeaves() { 18 | return leaves; 19 | } 20 | public void setLeaves(Integer leaves) { 21 | this.leaves = leaves; 22 | } 23 | 24 | @Column(name = "SALARY", nullable = false, length = 100) 25 | public Integer getSalary() { 26 | return salary; 27 | } 28 | public void setSalary(Integer salary) { 29 | this.salary = salary; 30 | } 31 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/classtableinheritance/service/EmployeeService.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.classtableinheritance.service; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.classtableinheritance.dto.EmployeeDto; 6 | 7 | public interface EmployeeService { 8 | public void create(EmployeeDto employeeDto) ; 9 | public List findAll(); 10 | public EmployeeDto findById(Integer id); 11 | public void remove(Integer employeeId); 12 | public void edit(EmployeeDto employeeDto); 13 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/concretetableinheritance/dao/EstateDao.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.concretetableinheritance.dao; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.concretetableinheritance.entity.Estate; 6 | 7 | public interface EstateDao { 8 | public void insert(Estate estate) ; 9 | public List getAll(); 10 | public Estate getById(Integer id); 11 | public void delete(Estate estate) ; 12 | public void update(Estate estate); 13 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/concretetableinheritance/dao/impl/EstateDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.concretetableinheritance.dao.impl; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Repository; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import com.oodd.spring.concretetableinheritance.dao.EstateDao; 11 | import com.oodd.spring.concretetableinheritance.entity.Estate; 12 | 13 | @Repository 14 | @Transactional 15 | public class EstateDaoImpl implements EstateDao { 16 | @Autowired 17 | private SessionFactory sessionFactory ; 18 | @Override 19 | public void insert(Estate estate) { 20 | sessionFactory.getCurrentSession().save(estate); 21 | } 22 | @SuppressWarnings("unchecked") 23 | @Override 24 | public List getAll() { 25 | return sessionFactory.getCurrentSession().createQuery("select estate from Estate estate order by estate.id desc").list(); 26 | } 27 | @Override 28 | public Estate getById(Integer id) { 29 | return (Estate) sessionFactory.getCurrentSession().get(Estate.class, id); 30 | } 31 | @Override 32 | public void delete(Estate estate) { 33 | sessionFactory.getCurrentSession().delete(estate); 34 | } 35 | @Override 36 | public void update(Estate estate) { 37 | sessionFactory.getCurrentSession().merge(estate); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/concretetableinheritance/dto/BuildingDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.concretetableinheritance.dto; 2 | 3 | import com.fasterxml.jackson.annotation.JsonProperty; 4 | 5 | public class BuildingDto extends EstateDto { 6 | 7 | @JsonProperty("floors") 8 | private Integer floors; 9 | 10 | public Integer getFloors() { 11 | return floors; 12 | } 13 | 14 | public void setFloors(Integer floors) { 15 | this.floors = floors; 16 | } 17 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/concretetableinheritance/dto/EstateDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.concretetableinheritance.dto; 2 | 3 | import com.fasterxml.jackson.annotation.JsonSubTypes; 4 | import com.fasterxml.jackson.annotation.JsonSubTypes.Type; 5 | import com.fasterxml.jackson.annotation.JsonTypeInfo; 6 | 7 | @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type") 8 | @JsonSubTypes({ 9 | @Type(value = BuildingDto.class, name = "building"), 10 | @Type(value = LandDto.class, name = "land") }) 11 | public class EstateDto { 12 | private Integer id; 13 | private String name; 14 | 15 | public Integer getId() { 16 | return id; 17 | } 18 | public void setId(Integer id) { 19 | this.id = id; 20 | } 21 | public String getName() { 22 | return name; 23 | } 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/concretetableinheritance/dto/LandDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.concretetableinheritance.dto; 2 | 3 | import com.fasterxml.jackson.annotation.JsonProperty; 4 | 5 | public class LandDto extends EstateDto { 6 | 7 | @JsonProperty("area") 8 | private Integer area; 9 | 10 | public Integer getArea() { 11 | return area; 12 | } 13 | 14 | public void setArea(Integer area) { 15 | this.area = area; 16 | } 17 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/concretetableinheritance/entity/Building.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.concretetableinheritance.entity; 2 | 3 | import javax.persistence.AttributeOverride; 4 | import javax.persistence.AttributeOverrides; 5 | import javax.persistence.Column; 6 | import javax.persistence.Entity; 7 | import javax.persistence.Table; 8 | 9 | @Entity 10 | @Table(name="BUILDING") 11 | @AttributeOverrides({ 12 | @AttributeOverride(name="id", column=@Column(name="ID")), 13 | @AttributeOverride(name="name", column=@Column(name="NAME")) 14 | }) 15 | public class Building extends Estate { 16 | 17 | private Integer floors; 18 | 19 | @Column(name = "FLOORS", nullable = false, length = 100) 20 | public Integer getFloors() { 21 | return floors; 22 | } 23 | 24 | public void setFloors(Integer floors) { 25 | this.floors = floors; 26 | } 27 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/concretetableinheritance/entity/Estate.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.concretetableinheritance.entity; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Id; 8 | import javax.persistence.Inheritance; 9 | import javax.persistence.InheritanceType; 10 | import javax.persistence.Table; 11 | 12 | @Entity 13 | @Table(name = "ESTATE") 14 | @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 15 | public class Estate { 16 | 17 | private Integer id; 18 | private String name; 19 | 20 | @Id 21 | @GeneratedValue(strategy = GenerationType.TABLE) 22 | @Column(name = "ID") 23 | public Integer getId() { 24 | return id; 25 | } 26 | 27 | public void setId(Integer id) { 28 | this.id = id; 29 | } 30 | 31 | @Column(name = "NAME", nullable = false, length = 100) 32 | public String getName() { 33 | return name; 34 | } 35 | 36 | public void setName(String name) { 37 | this.name = name; 38 | } 39 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/concretetableinheritance/entity/Land.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.concretetableinheritance.entity; 2 | 3 | import javax.persistence.AttributeOverride; 4 | import javax.persistence.AttributeOverrides; 5 | import javax.persistence.Column; 6 | import javax.persistence.Entity; 7 | import javax.persistence.Table; 8 | 9 | @Entity 10 | @Table(name="LAND") 11 | @AttributeOverrides({ 12 | @AttributeOverride(name="id", column=@Column(name="ID")), 13 | @AttributeOverride(name="name", column=@Column(name="NAME")) 14 | }) 15 | public class Land extends Estate { 16 | 17 | private Integer area; 18 | 19 | @Column(name = "AREA", nullable = false, length = 100) 20 | public Integer getArea() { 21 | return area; 22 | } 23 | 24 | public void setArea(Integer area) { 25 | this.area = area; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/concretetableinheritance/mock/EstateInMemoryDB.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.concretetableinheritance.mock; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.oodd.spring.concretetableinheritance.dto.BuildingDto; 7 | import com.oodd.spring.concretetableinheritance.dto.EstateDto; 8 | import com.oodd.spring.concretetableinheritance.dto.LandDto; 9 | 10 | public enum EstateInMemoryDB { 11 | 12 | INSTANCE; 13 | 14 | private static List list = new ArrayList(); 15 | private static Integer lastId = 0; 16 | 17 | public Integer getId() { 18 | return ++lastId; 19 | } 20 | 21 | public void add(EstateDto estateDto) { 22 | estateDto.setId(getId()); 23 | list.add(estateDto); 24 | } 25 | 26 | public void edit(EstateDto estateDto) { 27 | for (EstateDto dto:list) { 28 | if (dto.getId()==estateDto.getId()) { 29 | dto.setName(estateDto.getName()); 30 | if(estateDto instanceof BuildingDto) { 31 | ((BuildingDto)dto).setFloors(((BuildingDto)estateDto).getFloors()); 32 | } else if(estateDto instanceof LandDto) { 33 | ((LandDto)dto).setArea(((LandDto)estateDto).getArea()); 34 | } 35 | } 36 | } 37 | } 38 | 39 | public void remove(Integer id) { 40 | EstateDto toRemove = null; 41 | for (EstateDto dto:list) 42 | if (dto.getId()==id) toRemove = dto; 43 | if (toRemove!=null) list.remove(toRemove); 44 | } 45 | 46 | public List findAll() { 47 | return list; 48 | } 49 | 50 | public EstateDto findById(Integer id) { 51 | for (EstateDto dto:list) { 52 | if (dto.getId()==id) 53 | return dto; 54 | } 55 | return null; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/concretetableinheritance/service/EstateService.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.concretetableinheritance.service; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.concretetableinheritance.dto.EstateDto; 6 | 7 | public interface EstateService { 8 | public void create(EstateDto estateDto ) ; 9 | public List findAll(); 10 | public EstateDto findById(Integer id); 11 | public void remove(Integer estateId); 12 | public void edit(EstateDto estateDto); 13 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectional/dao/AccountDao.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectional.dao; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanybidirectional.entity.Account; 6 | 7 | public interface AccountDao { 8 | public void insert(Account account); 9 | public List getAll(); 10 | public Account getById(Integer id); 11 | public void delete(Account account) ; 12 | public void update(Account account); 13 | } 14 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectional/dao/ClientAccountDao.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectional.dao; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanybidirectional.entity.Client; 6 | 7 | public interface ClientAccountDao { 8 | public List getAll(); 9 | public List isPresent(Integer clientId, Integer accountId); 10 | } 11 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectional/dao/ClientDao.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectional.dao; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanybidirectional.entity.Client; 6 | 7 | public interface ClientDao { 8 | public void insert(Client client); 9 | public List getAll(); 10 | public Client getById(Integer id); 11 | public void delete(Client client) ; 12 | public void update(Client client); 13 | } 14 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectional/dao/impl/AccountDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectional.dao.impl; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Repository; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import com.oodd.spring.manytomanybidirectional.dao.AccountDao; 11 | import com.oodd.spring.manytomanybidirectional.entity.Account; 12 | import com.oodd.spring.manytomanybidirectional.entity.Client; 13 | 14 | @Repository 15 | @Transactional 16 | public class AccountDaoImpl implements AccountDao { 17 | 18 | @Autowired 19 | private SessionFactory sessionFactory ; 20 | 21 | @Override 22 | public void insert(Account account) { 23 | sessionFactory.getCurrentSession().save(account); 24 | } 25 | 26 | @SuppressWarnings("unchecked") 27 | @Override 28 | public List getAll() { 29 | return sessionFactory.getCurrentSession().createQuery("select account from Account account order by account.id desc").list(); 30 | } 31 | 32 | @Override 33 | public Account getById(Integer id) { 34 | return (Account) sessionFactory.getCurrentSession().get(Account.class,id); 35 | } 36 | 37 | @Override 38 | public void delete(Account account) { 39 | for(Client client : account.getClients()) { 40 | client.getAccounts().remove(account); 41 | } 42 | sessionFactory.getCurrentSession().delete(account); 43 | } 44 | 45 | @Override 46 | public void update(Account account) { 47 | sessionFactory.getCurrentSession().merge(account); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectional/dao/impl/ClientAccountDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectional.dao.impl; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.Query; 6 | import org.hibernate.SessionFactory; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Repository; 9 | import org.springframework.transaction.annotation.Transactional; 10 | 11 | import com.oodd.spring.manytomanybidirectional.dao.ClientAccountDao; 12 | import com.oodd.spring.manytomanybidirectional.entity.Client; 13 | 14 | @Repository 15 | @Transactional 16 | public class ClientAccountDaoImpl implements ClientAccountDao { 17 | 18 | @Autowired 19 | private SessionFactory sessionFactory ; 20 | 21 | @SuppressWarnings("unchecked") 22 | @Override 23 | public List getAll() { 24 | return sessionFactory.getCurrentSession().createQuery("select distinct c from Client c join c.accounts a").list(); 25 | } 26 | 27 | @SuppressWarnings("unchecked") 28 | @Override 29 | public List isPresent(Integer clientid, Integer accountid) { 30 | String hql = "select distinct c from Client c join c.accounts a where c.id=:clientid and a.id=:accountid"; 31 | Query query = sessionFactory.getCurrentSession().createQuery(hql); 32 | query.setParameter("clientid", clientid); 33 | query.setParameter("accountid", accountid); 34 | return query.list(); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectional/dao/impl/ClientDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectional.dao.impl; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Repository; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import com.oodd.spring.manytomanybidirectional.dao.ClientDao; 11 | import com.oodd.spring.manytomanybidirectional.entity.Client; 12 | 13 | @Repository 14 | @Transactional 15 | public class ClientDaoImpl implements ClientDao { 16 | 17 | @Autowired 18 | private SessionFactory sessionFactory ; 19 | 20 | @Override 21 | public void insert(Client client) { 22 | sessionFactory.getCurrentSession().save(client); 23 | } 24 | 25 | @SuppressWarnings("unchecked") 26 | @Override 27 | public List getAll() { 28 | return sessionFactory.getCurrentSession().createQuery("select client from Client client order by client.id desc").list(); 29 | } 30 | 31 | @Override 32 | public Client getById(Integer id) { 33 | return (Client) sessionFactory.getCurrentSession().get(Client.class,id); 34 | } 35 | 36 | @Override 37 | public void delete(Client client) { 38 | sessionFactory.getCurrentSession().delete(client); 39 | } 40 | 41 | @Override 42 | public void update(Client client) { 43 | sessionFactory.getCurrentSession().merge(client); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectional/dto/AccountDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectional.dto; 2 | 3 | public class AccountDto { 4 | private Integer id; 5 | private String number; 6 | 7 | public Integer getId() { 8 | return id; 9 | } 10 | public void setId(Integer id) { 11 | this.id = id; 12 | } 13 | public String getNumber() { 14 | return number; 15 | } 16 | public void setNumber(String number) { 17 | this.number = number; 18 | } 19 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectional/dto/ClientAccountDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectional.dto; 2 | 3 | 4 | public class ClientAccountDto { 5 | 6 | private ClientDto clientDto; 7 | private AccountDto accountDto; 8 | 9 | public ClientDto getClientDto() { 10 | return clientDto; 11 | } 12 | public void setClientDto(ClientDto clientDto) { 13 | this.clientDto = clientDto; 14 | } 15 | public AccountDto getAccountDto() { 16 | return accountDto; 17 | } 18 | public void setAccountDto(AccountDto accountDto) { 19 | this.accountDto = accountDto; 20 | } 21 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectional/dto/ClientDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectional.dto; 2 | 3 | public class ClientDto { 4 | private Integer id; 5 | private String name; 6 | 7 | public Integer getId() { 8 | return id; 9 | } 10 | public void setId(Integer id) { 11 | this.id = id; 12 | } 13 | public String getName() { 14 | return name; 15 | } 16 | public void setName(String name) { 17 | this.name = name; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectional/entity/Account.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectional.entity; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | import javax.persistence.Column; 7 | import javax.persistence.Entity; 8 | import javax.persistence.FetchType; 9 | import javax.persistence.GeneratedValue; 10 | import javax.persistence.GenerationType; 11 | import javax.persistence.Id; 12 | import javax.persistence.ManyToMany; 13 | import javax.persistence.Table; 14 | 15 | @Entity 16 | @Table(name="ACCOUNT") 17 | public class Account { 18 | 19 | 20 | private Integer id; 21 | private String number; 22 | private Set clients = new HashSet(); 23 | 24 | @Id 25 | @GeneratedValue(strategy=GenerationType.AUTO) 26 | @Column(name="ID") 27 | public Integer getId() { 28 | return id; 29 | } 30 | public void setId(Integer id) { 31 | this.id = id; 32 | } 33 | 34 | @Column(name="NUMBER", nullable=false, length=100) 35 | public String getNumber() { 36 | return number; 37 | } 38 | public void setNumber(String number) { 39 | this.number = number; 40 | } 41 | 42 | @ManyToMany(fetch = FetchType.LAZY, mappedBy = "accounts") 43 | public Set getClients() { 44 | return clients; 45 | } 46 | public void setClients(Set clients) { 47 | this.clients = clients; 48 | } 49 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectional/entity/Client.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectional.entity; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | import javax.persistence.CascadeType; 7 | import javax.persistence.Column; 8 | import javax.persistence.Entity; 9 | import javax.persistence.FetchType; 10 | import javax.persistence.GeneratedValue; 11 | import javax.persistence.GenerationType; 12 | import javax.persistence.Id; 13 | import javax.persistence.JoinColumn; 14 | import javax.persistence.JoinTable; 15 | import javax.persistence.ManyToMany; 16 | import javax.persistence.Table; 17 | 18 | @Entity 19 | @Table(name="CLIENT") 20 | public class Client { 21 | 22 | private Integer id; 23 | private String name; 24 | private Set accounts = new HashSet(); 25 | 26 | @Id 27 | @GeneratedValue(strategy=GenerationType.AUTO) 28 | @Column(name="ID") 29 | public Integer getId() { 30 | return id; 31 | } 32 | public void setId(Integer id) { 33 | this.id = id; 34 | } 35 | 36 | @Column(name="NAME", nullable=false, length=100) 37 | public String getName() { 38 | return name; 39 | } 40 | public void setName(String name) { 41 | this.name = name; 42 | } 43 | 44 | @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY) 45 | @JoinTable( name = "CLIENT_ACCOUNT", 46 | joinColumns = @JoinColumn(name = "CLIENT_ID", referencedColumnName="ID"), 47 | inverseJoinColumns = @JoinColumn(name = "ACCOUNT_ID", referencedColumnName="ID")) 48 | public Set getAccounts() { 49 | return accounts; 50 | } 51 | public void setAccounts(Set accounts) { 52 | this.accounts = accounts; 53 | } 54 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectional/mapper/AccountMapper.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectional.mapper; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import com.oodd.spring.manytomanybidirectional.dto.AccountDto; 6 | import com.oodd.spring.manytomanybidirectional.entity.Account; 7 | 8 | @Component 9 | public class AccountMapper { 10 | public Account mapDtoToEntity(AccountDto accountDto) { 11 | Account account = new Account(); 12 | if(null != accountDto.getId()) account.setId(accountDto.getId()); 13 | if(null != accountDto.getNumber()) account.setNumber(accountDto.getNumber()); 14 | return account; 15 | } 16 | 17 | public AccountDto mapEntityToDto(Account account) { 18 | AccountDto AccountDto = new AccountDto(); 19 | if(null != account.getId()) AccountDto.setId(account.getId()); 20 | if(null != account.getNumber()) AccountDto.setNumber(account.getNumber()); 21 | return AccountDto; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectional/mapper/ClientMapper.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectional.mapper; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import com.oodd.spring.manytomanybidirectional.dto.ClientDto; 6 | import com.oodd.spring.manytomanybidirectional.entity.Client; 7 | 8 | @Component 9 | public class ClientMapper { 10 | 11 | public Client mapDtoToEntity(ClientDto clientDto) { 12 | Client client = new Client(); 13 | if(null != clientDto.getId()) client.setId(clientDto.getId()); 14 | if(null != clientDto.getName()) client.setName(clientDto.getName()); 15 | return client; 16 | } 17 | 18 | public ClientDto mapEntityToDto(Client client) { 19 | ClientDto clientDto = new ClientDto(); 20 | if(null != client.getId()) clientDto.setId(client.getId()); 21 | if(null != client.getName()) clientDto.setName(client.getName()); 22 | return clientDto; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectional/mock/ClientAccountInMemoryDB.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectional.mock; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.oodd.spring.manytomanybidirectional.dto.ClientAccountDto; 7 | 8 | public enum ClientAccountInMemoryDB { 9 | 10 | INSTANCE; 11 | 12 | private static List list = new ArrayList(); 13 | 14 | public void add(ClientAccountDto clientAccountDto) { 15 | list.add(clientAccountDto); 16 | } 17 | 18 | public boolean isPresent(ClientAccountDto clientAccountDto) { 19 | for (ClientAccountDto dto:list) { 20 | if (dto.getClientDto().getId() == clientAccountDto.getClientDto().getId() 21 | && dto.getAccountDto().getId() == clientAccountDto.getAccountDto().getId()) { 22 | return true; 23 | } 24 | } 25 | return false; 26 | } 27 | 28 | public void remove(ClientAccountDto clientAccountDto) { 29 | ClientAccountDto toRemove = null; 30 | for (ClientAccountDto dto:list) { 31 | if (dto.getClientDto().getId()==clientAccountDto.getClientDto().getId() 32 | && dto.getAccountDto().getId()==clientAccountDto.getAccountDto().getId()) { 33 | toRemove = dto; 34 | } 35 | } 36 | if (toRemove!=null) list.remove(toRemove); 37 | } 38 | 39 | public List findAll() { 40 | return list; 41 | } 42 | public void setList(List list) { 43 | ClientAccountInMemoryDB.list = list; 44 | } 45 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectional/mock/ClientAccountMockController.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectional.mock; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.http.HttpStatus; 6 | import org.springframework.stereotype.Controller; 7 | import org.springframework.web.bind.annotation.RequestBody; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RequestMethod; 10 | import org.springframework.web.bind.annotation.ResponseBody; 11 | import org.springframework.web.bind.annotation.ResponseStatus; 12 | 13 | import com.oodd.spring.manytomanybidirectional.dto.ClientAccountDto; 14 | 15 | @Controller 16 | @RequestMapping(value="/manytomanybidirectional/clientaccount/mock") 17 | public class ClientAccountMockController { 18 | 19 | @RequestMapping(value="/findAll", method=RequestMethod.GET) 20 | public @ResponseBody List findAll(){ 21 | return ClientAccountInMemoryDB.INSTANCE.findAll(); 22 | } 23 | @RequestMapping(value="/create", method=RequestMethod.POST) 24 | @ResponseBody 25 | public void create(@RequestBody ClientAccountDto clientAccountDto){ 26 | ClientAccountInMemoryDB.INSTANCE.add(clientAccountDto); 27 | } 28 | @RequestMapping(value="/remove", method=RequestMethod.POST) 29 | @ResponseStatus(value = HttpStatus.NO_CONTENT) 30 | public void remove(@RequestBody ClientAccountDto clientAccountDto){ 31 | ClientAccountInMemoryDB.INSTANCE.remove(clientAccountDto); 32 | } 33 | @RequestMapping(value="/isPresent", method=RequestMethod.POST) 34 | public @ResponseBody boolean isPresent(@RequestBody ClientAccountDto clientAccountDto){ 35 | return ClientAccountInMemoryDB.INSTANCE.isPresent(clientAccountDto); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectional/service/AccountService.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectional.service; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanybidirectional.dto.AccountDto; 6 | 7 | public interface AccountService { 8 | public void create(AccountDto accountDto) ; 9 | public List findAll(); 10 | public AccountDto findById(Integer id); 11 | public void remove(Integer id); 12 | public void edit(AccountDto accountDto); 13 | } 14 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectional/service/ClientAccountService.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectional.service; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanybidirectional.dto.ClientAccountDto; 6 | 7 | public interface ClientAccountService { 8 | public boolean isPresent(ClientAccountDto clientAccountDto); 9 | public List findAll(); 10 | public void create(ClientAccountDto clientAccountDto); 11 | public void remove(ClientAccountDto clientAccountDto); 12 | } 13 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectional/service/ClientService.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectional.service; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanybidirectional.dto.ClientDto; 6 | 7 | public interface ClientService { 8 | public void create(ClientDto clientDto) ; 9 | public List findAll(); 10 | public ClientDto findById(Integer id); 11 | public void remove(Integer id); 12 | public void edit(ClientDto clientDto); 13 | } 14 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectionalwithjoinattribute/dao/AuthorDao.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectionalwithjoinattribute.dao; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanybidirectionalwithjoinattribute.entity.Author; 6 | 7 | public interface AuthorDao { 8 | public void insert(Author author); 9 | public List getAll(); 10 | public Author getById(Integer id); 11 | public void delete(Author author) ; 12 | public void update(Author author); 13 | } 14 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectionalwithjoinattribute/dao/ManuscriptAuthorDao.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectionalwithjoinattribute.dao; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanybidirectionalwithjoinattribute.entity.ManuscriptAuthor; 6 | 7 | public interface ManuscriptAuthorDao { 8 | public List getAll(); 9 | public List isPresent(Integer manuscriptId, Integer authorId); 10 | public void delete(ManuscriptAuthor manuscriptAuthor); 11 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectionalwithjoinattribute/dao/ManuscriptDao.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectionalwithjoinattribute.dao; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanybidirectionalwithjoinattribute.entity.Manuscript; 6 | 7 | public interface ManuscriptDao { 8 | public void insert(Manuscript manuscript); 9 | public List getAll(); 10 | public Manuscript getById(Integer id); 11 | public void delete(Manuscript manuscript) ; 12 | public void update(Manuscript manuscript); 13 | } 14 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectionalwithjoinattribute/dao/impl/AuthorDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectionalwithjoinattribute.dao.impl; 2 | 3 | import java.util.List; 4 | import java.util.Set; 5 | 6 | import org.hibernate.SessionFactory; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Service; 9 | import org.springframework.transaction.annotation.Transactional; 10 | 11 | import com.oodd.spring.manytomanybidirectionalwithjoinattribute.dao.AuthorDao; 12 | import com.oodd.spring.manytomanybidirectionalwithjoinattribute.entity.Author; 13 | import com.oodd.spring.manytomanybidirectionalwithjoinattribute.entity.ManuscriptAuthor; 14 | 15 | @Service 16 | @Transactional 17 | public class AuthorDaoImpl implements AuthorDao { 18 | 19 | @Autowired 20 | private SessionFactory sessionFactory ; 21 | 22 | @Override 23 | public void insert(Author author) { 24 | sessionFactory.getCurrentSession().save(author); 25 | } 26 | 27 | @SuppressWarnings("unchecked") 28 | @Override 29 | public List getAll() { 30 | return sessionFactory.getCurrentSession().createQuery("select author from Author author order by author.id desc").list(); 31 | } 32 | 33 | @Override 34 | public Author getById(Integer id) { 35 | return (Author) sessionFactory.getCurrentSession().get(Author.class,id); 36 | } 37 | 38 | @Override 39 | public void delete(Author author) { 40 | Set manuscriptAuthors = author.getManuscriptAuthors(); 41 | for(ManuscriptAuthor manuscriptAuthor : manuscriptAuthors) { 42 | sessionFactory.getCurrentSession().delete(manuscriptAuthor); 43 | } 44 | sessionFactory.getCurrentSession().delete(author); 45 | } 46 | 47 | @Override 48 | public void update(Author author) { 49 | sessionFactory.getCurrentSession().merge(author); 50 | } 51 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectionalwithjoinattribute/dao/impl/ManuscriptAuthorDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectionalwithjoinattribute.dao.impl; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.Query; 6 | import org.hibernate.SessionFactory; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Repository; 9 | import org.springframework.transaction.annotation.Transactional; 10 | 11 | import com.oodd.spring.manytomanybidirectionalwithjoinattribute.dao.ManuscriptAuthorDao; 12 | import com.oodd.spring.manytomanybidirectionalwithjoinattribute.entity.ManuscriptAuthor; 13 | 14 | @Repository 15 | @Transactional 16 | public class ManuscriptAuthorDaoImpl implements ManuscriptAuthorDao { 17 | 18 | @Autowired 19 | private SessionFactory sessionFactory ; 20 | 21 | @SuppressWarnings("unchecked") 22 | @Override 23 | public List getAll() { 24 | return sessionFactory.getCurrentSession().createQuery("select distinct ma from ManuscriptAuthor ma").list(); 25 | } 26 | 27 | @SuppressWarnings("unchecked") 28 | @Override 29 | public List isPresent(Integer manuscriptId, Integer authorId) { 30 | String hql = "select distinct ma from ManuscriptAuthor ma where ma.manuscriptAuthorId.manuscript.id=:manuscriptId and ma.manuscriptAuthorId.author.id=:authorId"; 31 | Query query = sessionFactory.getCurrentSession().createQuery(hql); 32 | query.setParameter("manuscriptId", manuscriptId); 33 | query.setParameter("authorId", authorId); 34 | return query.list(); 35 | } 36 | 37 | @Override 38 | public void delete(ManuscriptAuthor manuscriptAuthor) { 39 | sessionFactory.getCurrentSession().delete(manuscriptAuthor); 40 | } 41 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectionalwithjoinattribute/dao/impl/ManuscriptDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectionalwithjoinattribute.dao.impl; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import com.oodd.spring.manytomanybidirectionalwithjoinattribute.dao.ManuscriptDao; 11 | import com.oodd.spring.manytomanybidirectionalwithjoinattribute.entity.Manuscript; 12 | 13 | @Service 14 | @Transactional 15 | public class ManuscriptDaoImpl implements ManuscriptDao { 16 | 17 | @Autowired 18 | private SessionFactory sessionFactory ; 19 | 20 | @Override 21 | public void insert(Manuscript manuscript) { 22 | sessionFactory.getCurrentSession().save(manuscript); 23 | } 24 | 25 | @SuppressWarnings("unchecked") 26 | @Override 27 | public List getAll() { 28 | return sessionFactory.getCurrentSession().createQuery("select manuscript from Manuscript manuscript order by manuscript.id desc").list(); 29 | } 30 | 31 | @Override 32 | public Manuscript getById(Integer id) { 33 | return (Manuscript) sessionFactory.getCurrentSession().get(Manuscript.class,id); 34 | } 35 | 36 | @Override 37 | public void delete(Manuscript manuscript) { 38 | sessionFactory.getCurrentSession().delete(manuscript); 39 | } 40 | 41 | @Override 42 | public void update(Manuscript manuscript) { 43 | sessionFactory.getCurrentSession().merge(manuscript); 44 | } 45 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectionalwithjoinattribute/dto/AuthorDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectionalwithjoinattribute.dto; 2 | 3 | public class AuthorDto { 4 | 5 | private Integer id; 6 | private String name; 7 | 8 | public Integer getId() { 9 | return id; 10 | } 11 | public void setId(Integer id) { 12 | this.id = id; 13 | } 14 | public String getName() { 15 | return name; 16 | } 17 | public void setName(String name) { 18 | this.name = name; 19 | } 20 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectionalwithjoinattribute/dto/ManuscriptAuthorDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectionalwithjoinattribute.dto; 2 | 3 | 4 | public class ManuscriptAuthorDto { 5 | 6 | private ManuscriptDto manuscriptDto; 7 | private AuthorDto authorDto; 8 | private String publisher; 9 | 10 | 11 | public ManuscriptDto getManuscriptDto() { 12 | return manuscriptDto; 13 | } 14 | public void setManuscriptDto(ManuscriptDto manuscriptDto) { 15 | this.manuscriptDto = manuscriptDto; 16 | } 17 | public AuthorDto getAuthorDto() { 18 | return authorDto; 19 | } 20 | public void setAuthorDto(AuthorDto authorDto) { 21 | this.authorDto = authorDto; 22 | } 23 | public String getPublisher() { 24 | return publisher; 25 | } 26 | public void setPublisher(String publisher) { 27 | this.publisher = publisher; 28 | } 29 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectionalwithjoinattribute/dto/ManuscriptDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectionalwithjoinattribute.dto; 2 | 3 | public class ManuscriptDto { 4 | 5 | private Integer id; 6 | private String name; 7 | 8 | public Integer getId() { 9 | return id; 10 | } 11 | public void setId(Integer id) { 12 | this.id = id; 13 | } 14 | public String getName() { 15 | return name; 16 | } 17 | public void setName(String name) { 18 | this.name = name; 19 | } 20 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectionalwithjoinattribute/entity/Author.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectionalwithjoinattribute.entity; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | import javax.persistence.Column; 7 | import javax.persistence.Entity; 8 | import javax.persistence.FetchType; 9 | import javax.persistence.GeneratedValue; 10 | import javax.persistence.GenerationType; 11 | import javax.persistence.Id; 12 | import javax.persistence.OneToMany; 13 | import javax.persistence.Table; 14 | 15 | @Entity 16 | @Table(name="AUTHOR") 17 | public class Author { 18 | 19 | private Integer id; 20 | private String name; 21 | private Set manuscriptAuthors = new HashSet(0); 22 | 23 | @Id 24 | @GeneratedValue(strategy = GenerationType.AUTO) 25 | @Column(name="ID") 26 | public Integer getId() { 27 | return id; 28 | } 29 | public void setId(Integer id) { 30 | this.id = id; 31 | } 32 | 33 | @Column(name="NAME", nullable=false, length=100) 34 | public String getName() { 35 | return name; 36 | } 37 | public void setName(String name) { 38 | this.name = name; 39 | } 40 | 41 | @OneToMany(fetch = FetchType.LAZY, mappedBy = "manuscriptAuthorId.author") 42 | public Set getManuscriptAuthors() { 43 | return manuscriptAuthors; 44 | } 45 | public void setManuscriptAuthors(Set manuscriptAuthors) { 46 | this.manuscriptAuthors = manuscriptAuthors; 47 | } 48 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectionalwithjoinattribute/entity/Manuscript.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectionalwithjoinattribute.entity; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | import javax.persistence.CascadeType; 7 | import javax.persistence.Column; 8 | import javax.persistence.Entity; 9 | import javax.persistence.FetchType; 10 | import javax.persistence.GeneratedValue; 11 | import javax.persistence.GenerationType; 12 | import javax.persistence.Id; 13 | import javax.persistence.OneToMany; 14 | import javax.persistence.Table; 15 | 16 | @Entity 17 | @Table(name="MANUSCRIPT") 18 | public class Manuscript { 19 | 20 | private Integer id; 21 | private String name; 22 | private Set manuscriptAuthors = new HashSet(0); 23 | 24 | @Id 25 | @GeneratedValue(strategy = GenerationType.AUTO) 26 | @Column(name="ID") 27 | public Integer getId() { 28 | return id; 29 | } 30 | public void setId(Integer id) { 31 | this.id = id; 32 | } 33 | 34 | @Column(name="NAME", nullable=false, length=100) 35 | public String getName() { 36 | return name; 37 | } 38 | public void setName(String name) { 39 | this.name = name; 40 | } 41 | 42 | @OneToMany(fetch = FetchType.LAZY, mappedBy = "manuscriptAuthorId.manuscript", cascade=CascadeType.ALL) 43 | public Set getManuscriptAuthors() { 44 | return manuscriptAuthors; 45 | } 46 | public void setManuscriptAuthors(Set manuscriptAuthors) { 47 | this.manuscriptAuthors = manuscriptAuthors; 48 | } 49 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectionalwithjoinattribute/entity/ManuscriptAuthorId.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectionalwithjoinattribute.entity; 2 | 3 | import javax.persistence.Embeddable; 4 | import javax.persistence.ManyToOne; 5 | 6 | @Embeddable 7 | public class ManuscriptAuthorId implements java.io.Serializable { 8 | 9 | private static final long serialVersionUID = 8395881050435165891L; 10 | 11 | private Manuscript manuscript; 12 | private Author author; 13 | 14 | @ManyToOne 15 | public Manuscript getManuscript() { 16 | return manuscript; 17 | } 18 | public void setManuscript(Manuscript manuscript) { 19 | this.manuscript = manuscript; 20 | } 21 | 22 | @ManyToOne 23 | public Author getAuthor() { 24 | return author; 25 | } 26 | public void setAuthor(Author author) { 27 | this.author = author; 28 | } 29 | 30 | public boolean equals(Object o) { 31 | if (this == o) return true; 32 | if (o == null || getClass() != o.getClass()) return false; 33 | 34 | ManuscriptAuthorId that = (ManuscriptAuthorId) o; 35 | 36 | if (manuscript != null ? !manuscript.equals(that.manuscript) : that.manuscript != null) return false; 37 | if (author != null ? !author.equals(that.author) : that.author != null) 38 | return false; 39 | 40 | return true; 41 | } 42 | 43 | public int hashCode() { 44 | int result; 45 | result = (manuscript != null ? manuscript.hashCode() : 0); 46 | result = 31 * result + (author != null ? author.hashCode() : 0); 47 | return result; 48 | } 49 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectionalwithjoinattribute/mapper/AuthorMapper.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectionalwithjoinattribute.mapper; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import com.oodd.spring.manytomanybidirectionalwithjoinattribute.dto.AuthorDto; 6 | import com.oodd.spring.manytomanybidirectionalwithjoinattribute.entity.Author; 7 | 8 | @Component 9 | public class AuthorMapper { 10 | public Author mapDtoToEntity(AuthorDto authorDto) { 11 | Author author = new Author(); 12 | if(null != authorDto.getId()) author.setId(authorDto.getId()); 13 | if(null != authorDto.getName()) author.setName(authorDto.getName()); 14 | return author; 15 | } 16 | 17 | public AuthorDto mapEntityToDto(Author author) { 18 | AuthorDto authorDto = new AuthorDto(); 19 | if(null != author.getId()) authorDto.setId(author.getId()); 20 | if(null != author.getName()) authorDto.setName(author.getName()); 21 | return authorDto; 22 | } 23 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectionalwithjoinattribute/mapper/ManuscriptMapper.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectionalwithjoinattribute.mapper; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import com.oodd.spring.manytomanybidirectionalwithjoinattribute.dto.ManuscriptDto; 6 | import com.oodd.spring.manytomanybidirectionalwithjoinattribute.entity.Manuscript; 7 | 8 | @Component 9 | public class ManuscriptMapper { 10 | 11 | public Manuscript mapDtoToEntity(ManuscriptDto manuscriptDto) { 12 | Manuscript manuscript = new Manuscript(); 13 | if(null != manuscriptDto.getId()) manuscript.setId(manuscriptDto.getId()); 14 | if(null != manuscriptDto.getName()) manuscript.setName(manuscriptDto.getName()); 15 | return manuscript; 16 | } 17 | 18 | public ManuscriptDto mapEntityToDto(Manuscript manuscript) { 19 | ManuscriptDto manuscriptDto = new ManuscriptDto(); 20 | if(null != manuscript.getId()) manuscriptDto.setId(manuscript.getId()); 21 | if(null != manuscript.getName()) manuscriptDto.setName(manuscript.getName()); 22 | return manuscriptDto; 23 | } 24 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectionalwithjoinattribute/mock/ManuscriptAuthorInMemoryDB.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectionalwithjoinattribute.mock; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.oodd.spring.manytomanybidirectionalwithjoinattribute.dto.ManuscriptAuthorDto; 7 | 8 | public enum ManuscriptAuthorInMemoryDB { 9 | 10 | INSTANCE; 11 | 12 | private static List list = new ArrayList(); 13 | 14 | public void add(ManuscriptAuthorDto manuscriptAuthorDto) { 15 | list.add(manuscriptAuthorDto); 16 | } 17 | 18 | public boolean isPresent(ManuscriptAuthorDto manuscriptAuthorDto) { 19 | for (ManuscriptAuthorDto dto:list) { 20 | if (dto.getManuscriptDto().getId() == manuscriptAuthorDto.getManuscriptDto().getId() 21 | && dto.getAuthorDto().getId() == manuscriptAuthorDto.getAuthorDto().getId()) { 22 | return true; 23 | } 24 | } 25 | return false; 26 | } 27 | 28 | public void remove(ManuscriptAuthorDto manuscriptAuthorDto) { 29 | ManuscriptAuthorDto toRemove = null; 30 | for (ManuscriptAuthorDto dto:list) { 31 | if (dto.getManuscriptDto().getId()==manuscriptAuthorDto.getManuscriptDto().getId() 32 | && dto.getAuthorDto().getId()==manuscriptAuthorDto.getAuthorDto().getId()) { 33 | toRemove = dto; 34 | } 35 | } 36 | if (toRemove!=null) list.remove(toRemove); 37 | } 38 | 39 | public List findAll() { 40 | return list; 41 | } 42 | public void setList(List list) { 43 | ManuscriptAuthorInMemoryDB.list = list; 44 | } 45 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectionalwithjoinattribute/service/AuthorService.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectionalwithjoinattribute.service; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanybidirectionalwithjoinattribute.dto.AuthorDto; 6 | 7 | public interface AuthorService { 8 | public void create(AuthorDto authorDto) ; 9 | public List findAll(); 10 | public AuthorDto findById(Integer id); 11 | public void remove(Integer id); 12 | public void edit(AuthorDto authorDto); 13 | } 14 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectionalwithjoinattribute/service/ManuscriptAuthorService.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectionalwithjoinattribute.service; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanybidirectionalwithjoinattribute.dto.ManuscriptAuthorDto; 6 | 7 | public interface ManuscriptAuthorService { 8 | public boolean isPresent(ManuscriptAuthorDto manuscriptAuthorDto); 9 | public List findAll(); 10 | public void create(ManuscriptAuthorDto manuscriptAuthorDto); 11 | public void remove(ManuscriptAuthorDto manuscriptAuthorDto); 12 | } 13 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanybidirectionalwithjoinattribute/service/ManuscriptService.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectionalwithjoinattribute.service; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanybidirectionalwithjoinattribute.dto.ManuscriptDto; 6 | 7 | public interface ManuscriptService { 8 | public void create(ManuscriptDto manuscriptDto) ; 9 | public List findAll(); 10 | public ManuscriptDto findById(Integer id); 11 | public void remove(Integer id); 12 | public void edit(ManuscriptDto manuscriptDto); 13 | } 14 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreference/dao/MemberDao.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreference.dao; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanyselfreference.entity.Member; 6 | 7 | public interface MemberDao { 8 | public void insert(Member member); 9 | public List getAll(); 10 | public Member getById(Integer id); 11 | public void delete(Member member) ; 12 | public void update(Member member); 13 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreference/dao/MemberMemberDao.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreference.dao; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanyselfreference.entity.Member; 6 | 7 | public interface MemberMemberDao { 8 | public List getAll(); 9 | public List isPresent(Integer memberId1, Integer memberId2); 10 | } 11 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreference/dao/impl/MemberDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreference.dao.impl; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Repository; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import com.oodd.spring.manytomanyselfreference.dao.MemberDao; 11 | import com.oodd.spring.manytomanyselfreference.entity.Member; 12 | 13 | @Repository 14 | @Transactional 15 | public class MemberDaoImpl implements MemberDao { 16 | 17 | @Autowired 18 | private SessionFactory sessionFactory ; 19 | 20 | @Override 21 | public void insert(Member member) { 22 | sessionFactory.getCurrentSession().save(member); 23 | } 24 | 25 | @SuppressWarnings("unchecked") 26 | @Override 27 | public List getAll() { 28 | return sessionFactory.getCurrentSession().createQuery("select m from Member m order by m.id desc").list(); 29 | } 30 | 31 | @Override 32 | public Member getById(Integer id) { 33 | Member member = (Member)sessionFactory.getCurrentSession().get(Member.class,id); 34 | return member; 35 | } 36 | 37 | @Override 38 | public void delete(Member member) { 39 | sessionFactory.getCurrentSession().delete(member); 40 | } 41 | 42 | @Override 43 | public void update(Member member) { 44 | sessionFactory.getCurrentSession().merge(member); 45 | } 46 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreference/dao/impl/MemberMemberDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreference.dao.impl; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.Query; 6 | import org.hibernate.SessionFactory; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Repository; 9 | import org.springframework.transaction.annotation.Transactional; 10 | 11 | import com.oodd.spring.manytomanyselfreference.dao.MemberMemberDao; 12 | import com.oodd.spring.manytomanyselfreference.entity.Member; 13 | 14 | @Repository 15 | @Transactional 16 | public class MemberMemberDaoImpl implements MemberMemberDao { 17 | 18 | @Autowired 19 | private SessionFactory sessionFactory ; 20 | 21 | @SuppressWarnings("unchecked") 22 | @Override 23 | public List getAll() { 24 | return sessionFactory.getCurrentSession().createQuery("select distinct m from Member m join m.members1 m1").list(); 25 | } 26 | 27 | @SuppressWarnings("unchecked") 28 | @Override 29 | public List isPresent(Integer memberId1, Integer memberId2) { 30 | String hql = "select distinct m from Member m join m.members1 m1 where m.id=:memberId1 and m1.id=:memberId2"; 31 | Query query = sessionFactory.getCurrentSession().createQuery(hql); 32 | query.setParameter("memberId1", memberId1); 33 | query.setParameter("memberId2", memberId2); 34 | return query.list(); 35 | } 36 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreference/dto/MemberDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreference.dto; 2 | 3 | public class MemberDto { 4 | private Integer id; 5 | private String name; 6 | 7 | public Integer getId() { 8 | return id; 9 | } 10 | public void setId(Integer id) { 11 | this.id = id; 12 | } 13 | public String getName() { 14 | return name; 15 | } 16 | public void setName(String name) { 17 | this.name = name; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreference/dto/MemberMemberDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreference.dto; 2 | 3 | 4 | public class MemberMemberDto { 5 | private MemberDto memberId1; 6 | private MemberDto memberId2; 7 | 8 | public MemberDto getMemberId1() { 9 | return memberId1; 10 | } 11 | public void setMemberId1(MemberDto memberId1) { 12 | this.memberId1 = memberId1; 13 | } 14 | public MemberDto getMemberId2() { 15 | return memberId2; 16 | } 17 | public void setMemberId2(MemberDto memberId2) { 18 | this.memberId2 = memberId2; 19 | } 20 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreference/entity/Member.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreference.entity; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | import javax.persistence.Column; 7 | import javax.persistence.Entity; 8 | import javax.persistence.FetchType; 9 | import javax.persistence.GeneratedValue; 10 | import javax.persistence.GenerationType; 11 | import javax.persistence.Id; 12 | import javax.persistence.JoinColumn; 13 | import javax.persistence.JoinTable; 14 | import javax.persistence.ManyToMany; 15 | import javax.persistence.Table; 16 | 17 | @Entity 18 | @Table(name="MEMBER") 19 | public class Member { 20 | 21 | private Integer id; 22 | private String name; 23 | private Set members1 = new HashSet(); 24 | 25 | @Id 26 | @GeneratedValue(strategy=GenerationType.AUTO) 27 | @Column(name="ID") 28 | public Integer getId() { 29 | return id; 30 | } 31 | public void setId(Integer id) { 32 | this.id = id; 33 | } 34 | 35 | @Column(name="NAME", nullable=false, length=100) 36 | public String getName() { 37 | return name; 38 | } 39 | public void setName(String name) { 40 | this.name = name; 41 | } 42 | 43 | @ManyToMany(fetch = FetchType.LAZY) 44 | @JoinTable( name = "MEMBER_MEMBER", 45 | joinColumns = @JoinColumn(name = "MEMBER1_ID", referencedColumnName="ID"), 46 | inverseJoinColumns = @JoinColumn(name = "MEMBER2_ID", referencedColumnName="ID")) 47 | public Set getMembers1() { 48 | return members1; 49 | } 50 | public void setMembers1(Set members1) { 51 | this.members1 = members1; 52 | } 53 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreference/mapper/MemberMapper.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreference.mapper; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import com.oodd.spring.manytomanyselfreference.dto.MemberDto; 6 | import com.oodd.spring.manytomanyselfreference.entity.Member; 7 | 8 | @Component 9 | public class MemberMapper { 10 | 11 | public Member mapDtoToEntity(MemberDto memberDto) { 12 | Member member = new Member(); 13 | if(null != memberDto.getId()) member.setId(memberDto.getId()); 14 | if(null != memberDto.getName()) member.setName(memberDto.getName()); 15 | return member; 16 | } 17 | 18 | public MemberDto mapEntityToDto(Member member) { 19 | MemberDto memberDto = new MemberDto(); 20 | if(null != member.getId()) memberDto.setId(member.getId()); 21 | if(null != member.getName()) memberDto.setName(member.getName()); 22 | return memberDto; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreference/mock/MemberMemberInMemoryDB.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreference.mock; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.oodd.spring.manytomanyselfreference.dto.MemberMemberDto; 7 | 8 | public enum MemberMemberInMemoryDB { 9 | 10 | INSTANCE; 11 | 12 | private static List list = new ArrayList(); 13 | 14 | public void add(MemberMemberDto memberMemberDto) { 15 | list.add(memberMemberDto); 16 | } 17 | 18 | public boolean isPresent(MemberMemberDto memberMemberDto) { 19 | for (MemberMemberDto dto:list) { 20 | if (dto.getMemberId1().getId() == memberMemberDto.getMemberId1().getId() 21 | && dto.getMemberId2().getId() == memberMemberDto.getMemberId2().getId()) { 22 | return true; 23 | } else if (dto.getMemberId1().getId() == memberMemberDto.getMemberId2().getId() 24 | && dto.getMemberId2().getId() == memberMemberDto.getMemberId1().getId()) { 25 | return true; 26 | } 27 | 28 | } 29 | return false; 30 | } 31 | 32 | public void remove(MemberMemberDto memberMemberDto) { 33 | MemberMemberDto toRemove = null; 34 | for (MemberMemberDto dto:list) { 35 | if (dto.getMemberId1().getId()==memberMemberDto.getMemberId1().getId() 36 | && dto.getMemberId2().getId() == memberMemberDto.getMemberId2().getId()) { 37 | toRemove = dto; 38 | } 39 | } 40 | if (toRemove!=null) list.remove(toRemove); 41 | } 42 | 43 | public List findAll() { 44 | return list; 45 | } 46 | public void setList(List list) { 47 | MemberMemberInMemoryDB.list = list; 48 | } 49 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreference/mock/MemberMemberMockController.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreference.mock; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.http.HttpStatus; 6 | import org.springframework.stereotype.Controller; 7 | import org.springframework.web.bind.annotation.RequestBody; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RequestMethod; 10 | import org.springframework.web.bind.annotation.ResponseBody; 11 | import org.springframework.web.bind.annotation.ResponseStatus; 12 | 13 | import com.oodd.spring.manytomanyselfreference.dto.MemberMemberDto; 14 | 15 | @Controller 16 | @RequestMapping(value="/manytomanyselfreference/membermember/mock") 17 | public class MemberMemberMockController { 18 | 19 | @RequestMapping(value="/findAll", method=RequestMethod.GET) 20 | public @ResponseBody List findAll(){ 21 | return MemberMemberInMemoryDB.INSTANCE.findAll(); 22 | } 23 | @RequestMapping(value="/create", method=RequestMethod.POST) 24 | @ResponseBody 25 | public void create(@RequestBody MemberMemberDto memberMemberDto){ 26 | MemberMemberInMemoryDB.INSTANCE.add(memberMemberDto); 27 | } 28 | @RequestMapping(value="/remove", method=RequestMethod.POST) 29 | @ResponseStatus(value = HttpStatus.NO_CONTENT) 30 | public void remove(@RequestBody MemberMemberDto memberMemberDto){ 31 | MemberMemberInMemoryDB.INSTANCE.remove(memberMemberDto); 32 | } 33 | @RequestMapping(value="/isPresent", method=RequestMethod.POST) 34 | public @ResponseBody boolean isPresent(@RequestBody MemberMemberDto memberMemberDto){ 35 | return MemberMemberInMemoryDB.INSTANCE.isPresent(memberMemberDto); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreference/service/MemberMemberService.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreference.service; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanyselfreference.dto.MemberMemberDto; 6 | 7 | public interface MemberMemberService { 8 | public boolean isPresent(MemberMemberDto memberMemberDto); 9 | public List findAll(); 10 | public void create(MemberMemberDto memberMemberDto); 11 | public void remove(MemberMemberDto memberMemberDto); 12 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreference/service/MemberService.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreference.service; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanyselfreference.dto.MemberDto; 6 | 7 | public interface MemberService { 8 | public void create(MemberDto memberDto) ; 9 | public List findAll(); 10 | public MemberDto findById(Integer id); 11 | public void remove(Integer id); 12 | public void edit(MemberDto memberDto); 13 | } 14 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreferencewithjoinattribute/dao/WorkerDao.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreferencewithjoinattribute.dao; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanyselfreferencewithjoinattribute.entity.Worker; 6 | 7 | public interface WorkerDao { 8 | public void insert(Worker worker); 9 | public List getAll(); 10 | public Worker getById(Integer id); 11 | public void delete(Worker worker) ; 12 | public void update(Worker worker); 13 | } 14 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreferencewithjoinattribute/dao/WorkerWorkerDao.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreferencewithjoinattribute.dao; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanyselfreferencewithjoinattribute.entity.WorkerWorker; 6 | 7 | public interface WorkerWorkerDao { 8 | public List getAll(); 9 | public List isPresent(Integer workerId, Integer authorId); 10 | public void delete(WorkerWorker workerWorker); 11 | public void insert(WorkerWorker workerWorker); 12 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreferencewithjoinattribute/dao/impl/WorkerDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreferencewithjoinattribute.dao.impl; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import com.oodd.spring.manytomanyselfreferencewithjoinattribute.dao.WorkerDao; 11 | import com.oodd.spring.manytomanyselfreferencewithjoinattribute.entity.Worker; 12 | 13 | @Service 14 | @Transactional 15 | public class WorkerDaoImpl implements WorkerDao { 16 | 17 | @Autowired 18 | private SessionFactory sessionFactory ; 19 | 20 | @Override 21 | public void insert(Worker worker) { 22 | sessionFactory.getCurrentSession().save(worker); 23 | } 24 | 25 | @SuppressWarnings("unchecked") 26 | @Override 27 | public List getAll() { 28 | return sessionFactory.getCurrentSession().createQuery("select worker from Worker worker order by worker.id desc").list(); 29 | } 30 | 31 | @Override 32 | public Worker getById(Integer id) { 33 | return (Worker) sessionFactory.getCurrentSession().get(Worker.class,id); 34 | } 35 | 36 | @Override 37 | public void delete(Worker worker) { 38 | sessionFactory.getCurrentSession().delete(worker); 39 | } 40 | 41 | @Override 42 | public void update(Worker worker) { 43 | sessionFactory.getCurrentSession().merge(worker); 44 | } 45 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreferencewithjoinattribute/dao/impl/WorkerWorkerDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreferencewithjoinattribute.dao.impl; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.Query; 6 | import org.hibernate.SessionFactory; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Repository; 9 | import org.springframework.transaction.annotation.Transactional; 10 | 11 | import com.oodd.spring.manytomanyselfreferencewithjoinattribute.dao.WorkerWorkerDao; 12 | import com.oodd.spring.manytomanyselfreferencewithjoinattribute.entity.WorkerWorker; 13 | 14 | @Repository 15 | @Transactional 16 | public class WorkerWorkerDaoImpl implements WorkerWorkerDao { 17 | 18 | @Autowired 19 | private SessionFactory sessionFactory ; 20 | 21 | @SuppressWarnings("unchecked") 22 | @Override 23 | public List getAll() { 24 | return sessionFactory.getCurrentSession().createQuery("select distinct ww from WorkerWorker ww").list(); 25 | } 26 | 27 | @SuppressWarnings("unchecked") 28 | @Override 29 | public List isPresent(Integer workerId1, Integer workerId2) { 30 | String hql = "select distinct ww from WorkerWorker ww where ww.workerWorkerId.worker1.id=:workerId1 and ww.workerWorkerId.worker2.id=:workerId2"; 31 | Query query = sessionFactory.getCurrentSession().createQuery(hql); 32 | query.setParameter("workerId1", workerId1); 33 | query.setParameter("workerId2", workerId2); 34 | return query.list(); 35 | } 36 | 37 | @Override 38 | public void delete(WorkerWorker workerWorker) { 39 | sessionFactory.getCurrentSession().delete(workerWorker); 40 | } 41 | 42 | @Override 43 | public void insert(WorkerWorker workerWorker) { 44 | sessionFactory.getCurrentSession().save(workerWorker); 45 | } 46 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreferencewithjoinattribute/dto/WorkerDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreferencewithjoinattribute.dto; 2 | 3 | public class WorkerDto { 4 | 5 | private Integer id; 6 | private String name; 7 | 8 | public Integer getId() { 9 | return id; 10 | } 11 | public void setId(Integer id) { 12 | this.id = id; 13 | } 14 | public String getName() { 15 | return name; 16 | } 17 | public void setName(String name) { 18 | this.name = name; 19 | } 20 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreferencewithjoinattribute/dto/WorkerWorkerDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreferencewithjoinattribute.dto; 2 | 3 | 4 | public class WorkerWorkerDto { 5 | 6 | private WorkerDto workerId1; 7 | private WorkerDto workerId2; 8 | private String relationshipType; 9 | 10 | public WorkerDto getWorkerId1() { 11 | return workerId1; 12 | } 13 | public void setWorkerId1(WorkerDto workerId1) { 14 | this.workerId1 = workerId1; 15 | } 16 | public WorkerDto getWorkerId2() { 17 | return workerId2; 18 | } 19 | public void setWorkerId2(WorkerDto workerId2) { 20 | this.workerId2 = workerId2; 21 | } 22 | public String getRelationshipType() { 23 | return relationshipType; 24 | } 25 | public void setRelationshipType(String relationshipType) { 26 | this.relationshipType = relationshipType; 27 | } 28 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreferencewithjoinattribute/entity/Worker.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreferencewithjoinattribute.entity; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | import javax.persistence.CascadeType; 7 | import javax.persistence.Column; 8 | import javax.persistence.Entity; 9 | import javax.persistence.FetchType; 10 | import javax.persistence.GeneratedValue; 11 | import javax.persistence.GenerationType; 12 | import javax.persistence.Id; 13 | import javax.persistence.OneToMany; 14 | import javax.persistence.Table; 15 | 16 | @Entity 17 | @Table(name="WORKER") 18 | public class Worker { 19 | 20 | private Integer id; 21 | private String name; 22 | private Set workers1 = new HashSet(0); 23 | 24 | @Id 25 | @GeneratedValue(strategy=GenerationType.AUTO) 26 | @Column(name="ID") 27 | public Integer getId() { 28 | return id; 29 | } 30 | public void setId(Integer id) { 31 | this.id = id; 32 | } 33 | 34 | @Column(name="NAME", nullable=false, length=100) 35 | public String getName() { 36 | return name; 37 | } 38 | public void setName(String name) { 39 | this.name = name; 40 | } 41 | 42 | @OneToMany(fetch = FetchType.LAZY, mappedBy = "workerWorkerId.worker1", cascade=CascadeType.ALL) 43 | public Set getWorkers1() { 44 | return workers1; 45 | } 46 | public void setWorkers1(Set workers1) { 47 | this.workers1 = workers1; 48 | } 49 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreferencewithjoinattribute/entity/WorkerWorkerId.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreferencewithjoinattribute.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | import javax.persistence.Embeddable; 6 | import javax.persistence.ManyToOne; 7 | 8 | @Embeddable 9 | public class WorkerWorkerId implements Serializable { 10 | 11 | private static final long serialVersionUID = -2955127866598725414L; 12 | 13 | private Worker worker1; 14 | private Worker worker2; 15 | 16 | @ManyToOne 17 | public Worker getWorker1() { 18 | return worker1; 19 | } 20 | public void setWorker1(Worker worker1) { 21 | this.worker1 = worker1; 22 | } 23 | 24 | @ManyToOne 25 | public Worker getWorker2() { 26 | return worker2; 27 | } 28 | 29 | public void setWorker2(Worker worker2) { 30 | this.worker2 = worker2; 31 | } 32 | 33 | public boolean equals(Object o) { 34 | if (this == o) return true; 35 | if (o == null || getClass() != o.getClass()) return false; 36 | 37 | WorkerWorkerId that = (WorkerWorkerId) o; 38 | 39 | if (worker1 != null ? !worker1.equals(that.worker1) : that.worker1 != null) return false; 40 | if (worker2 != null ? !worker2.equals(that.worker2) : that.worker2 != null) 41 | return false; 42 | 43 | return true; 44 | } 45 | 46 | public int hashCode() { 47 | int result; 48 | result = (worker1 != null ? worker1.hashCode() : 0); 49 | result = 31 * result + (worker2 != null ? worker2.hashCode() : 0); 50 | return result; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreferencewithjoinattribute/mapper/WorkerMapper.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreferencewithjoinattribute.mapper; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import com.oodd.spring.manytomanyselfreferencewithjoinattribute.dto.WorkerDto; 6 | import com.oodd.spring.manytomanyselfreferencewithjoinattribute.entity.Worker; 7 | 8 | @Component 9 | public class WorkerMapper { 10 | 11 | public Worker mapDtoToEntity(WorkerDto workerDto) { 12 | Worker worker = new Worker(); 13 | if(null != workerDto.getId()) worker.setId(workerDto.getId()); 14 | if(null != workerDto.getName()) worker.setName(workerDto.getName()); 15 | return worker; 16 | } 17 | 18 | public WorkerDto mapEntityToDto(Worker worker) { 19 | WorkerDto workerDto = new WorkerDto(); 20 | if(null != worker.getId()) workerDto.setId(worker.getId()); 21 | if(null != worker.getName()) workerDto.setName(worker.getName()); 22 | return workerDto; 23 | } 24 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreferencewithjoinattribute/mock/WorkerWorkerInMemoryDB.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreferencewithjoinattribute.mock; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.oodd.spring.manytomanyselfreferencewithjoinattribute.dto.WorkerWorkerDto; 7 | 8 | public enum WorkerWorkerInMemoryDB { 9 | 10 | INSTANCE; 11 | 12 | private static List list = new ArrayList(); 13 | 14 | public void add(WorkerWorkerDto workerWorkerDto) { 15 | list.add(workerWorkerDto); 16 | } 17 | 18 | public boolean isPresent(WorkerWorkerDto workerWorkerDto) { 19 | for (WorkerWorkerDto dto:list) { 20 | if (dto.getWorkerId1().getId() == workerWorkerDto.getWorkerId1().getId() 21 | && dto.getWorkerId2().getId() == workerWorkerDto.getWorkerId2().getId()) { 22 | return true; 23 | } else if (dto.getWorkerId1().getId() == workerWorkerDto.getWorkerId2().getId() 24 | && dto.getWorkerId2().getId() == workerWorkerDto.getWorkerId1().getId()) { 25 | return true; 26 | } 27 | 28 | } 29 | return false; 30 | } 31 | 32 | public void remove(WorkerWorkerDto workerWorkerDto) { 33 | WorkerWorkerDto toRemove = null; 34 | for (WorkerWorkerDto dto:list) { 35 | if (dto.getWorkerId1().getId()==workerWorkerDto.getWorkerId1().getId() 36 | && dto.getWorkerId2().getId() == workerWorkerDto.getWorkerId2().getId()) { 37 | toRemove = dto; 38 | } 39 | } 40 | if (toRemove!=null) list.remove(toRemove); 41 | } 42 | 43 | public List findAll() { 44 | return list; 45 | } 46 | public void setList(List list) { 47 | WorkerWorkerInMemoryDB.list = list; 48 | } 49 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreferencewithjoinattribute/mock/WorkerWorkerMockController.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreferencewithjoinattribute.mock; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.http.HttpStatus; 6 | import org.springframework.stereotype.Controller; 7 | import org.springframework.web.bind.annotation.RequestBody; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RequestMethod; 10 | import org.springframework.web.bind.annotation.ResponseBody; 11 | import org.springframework.web.bind.annotation.ResponseStatus; 12 | 13 | import com.oodd.spring.manytomanyselfreferencewithjoinattribute.dto.WorkerWorkerDto; 14 | 15 | @Controller 16 | @RequestMapping(value="/manytomanyselfreferencewithjoinattribute/workerworker/mock") 17 | public class WorkerWorkerMockController { 18 | 19 | @RequestMapping(value="/findAll", method=RequestMethod.GET) 20 | public @ResponseBody List findAll(){ 21 | return WorkerWorkerInMemoryDB.INSTANCE.findAll(); 22 | } 23 | @RequestMapping(value="/create", method=RequestMethod.POST) 24 | @ResponseBody 25 | public void create(@RequestBody WorkerWorkerDto workerWorkerDto){ 26 | WorkerWorkerInMemoryDB.INSTANCE.add(workerWorkerDto); 27 | } 28 | @RequestMapping(value="/remove", method=RequestMethod.POST) 29 | @ResponseStatus(value = HttpStatus.NO_CONTENT) 30 | public void remove(@RequestBody WorkerWorkerDto workerWorkerDto){ 31 | WorkerWorkerInMemoryDB.INSTANCE.remove(workerWorkerDto); 32 | } 33 | @RequestMapping(value="/isPresent", method=RequestMethod.POST) 34 | public @ResponseBody boolean isPresent(@RequestBody WorkerWorkerDto workerWorkerDto){ 35 | return WorkerWorkerInMemoryDB.INSTANCE.isPresent(workerWorkerDto); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreferencewithjoinattribute/service/WorkerService.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreferencewithjoinattribute.service; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanyselfreferencewithjoinattribute.dto.WorkerDto; 6 | 7 | public interface WorkerService { 8 | public void create(WorkerDto manuscriptDto) ; 9 | public List findAll(); 10 | public WorkerDto findById(Integer id); 11 | public void remove(Integer id); 12 | public void edit(WorkerDto manuscriptDto); 13 | } 14 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyselfreferencewithjoinattribute/service/WorkerWorkerService.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreferencewithjoinattribute.service; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanyselfreferencewithjoinattribute.dto.WorkerWorkerDto; 6 | 7 | public interface WorkerWorkerService { 8 | public boolean isPresent(WorkerWorkerDto workerWorkerDto); 9 | public List findAll(); 10 | public void create(WorkerWorkerDto workerWorkerDto); 11 | public void remove(WorkerWorkerDto workerWorkerDto); 12 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyunidirectional/dao/GroupDao.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyunidirectional.dao; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanyunidirectional.entity.Group; 6 | 7 | public interface GroupDao { 8 | public void insert(Group group); 9 | public List getAll(); 10 | public Group getById(Integer id); 11 | public void delete(Group group) ; 12 | public void update(Group group); 13 | } 14 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyunidirectional/dao/UserDao.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyunidirectional.dao; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanyunidirectional.entity.User; 6 | 7 | public interface UserDao { 8 | public void insert(User user); 9 | public List getAll(); 10 | public User getById(Integer id); 11 | public void delete(User user) ; 12 | public void update(User user); 13 | } 14 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyunidirectional/dao/UserGroupDao.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyunidirectional.dao; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanyunidirectional.entity.User; 6 | 7 | public interface UserGroupDao { 8 | public List getAll(); 9 | public List isPresent(Integer userid, Integer groupid); 10 | } 11 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyunidirectional/dao/impl/GroupDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyunidirectional.dao.impl; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Repository; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import com.oodd.spring.manytomanyunidirectional.dao.GroupDao; 11 | import com.oodd.spring.manytomanyunidirectional.entity.Group; 12 | 13 | @Repository 14 | @Transactional 15 | public class GroupDaoImpl implements GroupDao { 16 | @Autowired 17 | private SessionFactory sessionFactory ; 18 | 19 | @Override 20 | public void insert(Group group) { 21 | sessionFactory.getCurrentSession().save(group); 22 | } 23 | 24 | @SuppressWarnings("unchecked") 25 | @Override 26 | public List getAll() { 27 | return sessionFactory.getCurrentSession().createQuery("select group from Group group order by group.id desc").list(); 28 | } 29 | 30 | @Override 31 | public Group getById(Integer id) { 32 | return (Group) sessionFactory.getCurrentSession().get(Group.class,id); 33 | } 34 | 35 | @Override 36 | public void delete(Group group) { 37 | sessionFactory.getCurrentSession().delete(group); 38 | } 39 | 40 | @Override 41 | public void update(Group group) { 42 | sessionFactory.getCurrentSession().merge(group); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyunidirectional/dao/impl/UserDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyunidirectional.dao.impl; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Repository; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import com.oodd.spring.manytomanyunidirectional.dao.UserDao; 11 | import com.oodd.spring.manytomanyunidirectional.entity.User; 12 | 13 | @Repository 14 | @Transactional 15 | public class UserDaoImpl implements UserDao { 16 | @Autowired 17 | private SessionFactory sessionFactory ; 18 | 19 | @Override 20 | public void insert(User user) { 21 | sessionFactory.getCurrentSession().save(user); 22 | } 23 | 24 | @SuppressWarnings("unchecked") 25 | @Override 26 | public List getAll() { 27 | return sessionFactory.getCurrentSession().createQuery("select user from User user order by user.id desc").list(); 28 | } 29 | 30 | @Override 31 | public User getById(Integer id) { 32 | return (User) sessionFactory.getCurrentSession().get(User.class,id); 33 | } 34 | 35 | @Override 36 | public void delete(User user) { 37 | sessionFactory.getCurrentSession().delete(user); 38 | } 39 | 40 | @Override 41 | public void update(User user) { 42 | sessionFactory.getCurrentSession().merge(user); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyunidirectional/dao/impl/UserGroupDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyunidirectional.dao.impl; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.Query; 6 | import org.hibernate.SessionFactory; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Repository; 9 | import org.springframework.transaction.annotation.Transactional; 10 | 11 | import com.oodd.spring.manytomanyunidirectional.dao.UserGroupDao; 12 | import com.oodd.spring.manytomanyunidirectional.entity.User; 13 | 14 | @Repository 15 | @Transactional 16 | public class UserGroupDaoImpl implements UserGroupDao { 17 | 18 | @Autowired 19 | private SessionFactory sessionFactory ; 20 | 21 | @Override 22 | @SuppressWarnings("unchecked") 23 | public List getAll() { 24 | return sessionFactory.getCurrentSession().createQuery("select distinct u from User u join u.groups g").list(); 25 | } 26 | 27 | @SuppressWarnings("unchecked") 28 | @Override 29 | public List isPresent(Integer userid, Integer groupid) { 30 | String hql = "select distinct u from User u join u.groups g where u.id=:userid and g.id=:groupid"; 31 | Query query = sessionFactory.getCurrentSession().createQuery(hql); 32 | query.setParameter("userid", userid); 33 | query.setParameter("groupid", groupid); 34 | return query.list(); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyunidirectional/dto/GroupDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyunidirectional.dto; 2 | 3 | public class GroupDto { 4 | 5 | private Integer id; 6 | private String name; 7 | 8 | public Integer getId() { 9 | return id; 10 | } 11 | public void setId(Integer id) { 12 | this.id = id; 13 | } 14 | public String getName() { 15 | return name; 16 | } 17 | public void setName(String name) { 18 | this.name = name; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyunidirectional/dto/UserDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyunidirectional.dto; 2 | 3 | public class UserDto { 4 | 5 | private Integer id; 6 | private String name; 7 | 8 | public Integer getId() { 9 | return id; 10 | } 11 | public void setId(Integer id) { 12 | this.id = id; 13 | } 14 | public String getName() { 15 | return name; 16 | } 17 | public void setName(String name) { 18 | this.name = name; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyunidirectional/dto/UserGroupDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyunidirectional.dto; 2 | 3 | 4 | public class UserGroupDto { 5 | 6 | private UserDto userDto; 7 | private GroupDto groupDto; 8 | 9 | public UserDto getUserDto() { 10 | return userDto; 11 | } 12 | public void setUserDto(UserDto userDto) { 13 | this.userDto = userDto; 14 | } 15 | public GroupDto getGroupDto() { 16 | return groupDto; 17 | } 18 | public void setGroupDto(GroupDto groupDto) { 19 | this.groupDto = groupDto; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyunidirectional/entity/Group.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyunidirectional.entity; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Id; 8 | import javax.persistence.Table; 9 | 10 | @Entity 11 | @Table(name="GROUPS") 12 | public class Group { 13 | 14 | private Integer id; 15 | private String name; 16 | 17 | @Id 18 | @GeneratedValue(strategy=GenerationType.AUTO) 19 | @Column(name="ID") 20 | public Integer getId() { 21 | return id; 22 | } 23 | public void setId(Integer id) { 24 | this.id = id; 25 | } 26 | 27 | @Column(name="NAME", nullable=false, length=100) 28 | public String getName() { 29 | return name; 30 | } 31 | public void setName(String name) { 32 | this.name = name; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyunidirectional/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyunidirectional.entity; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | import javax.persistence.CascadeType; 7 | import javax.persistence.Column; 8 | import javax.persistence.Entity; 9 | import javax.persistence.FetchType; 10 | import javax.persistence.GeneratedValue; 11 | import javax.persistence.GenerationType; 12 | import javax.persistence.Id; 13 | import javax.persistence.JoinColumn; 14 | import javax.persistence.JoinTable; 15 | import javax.persistence.ManyToMany; 16 | import javax.persistence.Table; 17 | 18 | @Entity 19 | @Table(name = "USER") 20 | public class User { 21 | 22 | private Integer id; 23 | private String name; 24 | private Set groups = new HashSet(); 25 | 26 | @Id 27 | @GeneratedValue(strategy=GenerationType.AUTO) 28 | @Column(name="ID") 29 | public Integer getId() { 30 | return id; 31 | } 32 | 33 | public void setId(Integer id) { 34 | this.id = id; 35 | } 36 | 37 | @Column(name="NAME", nullable=false, length=100) 38 | public String getName() { 39 | return name; 40 | } 41 | 42 | public void setName(String name) { 43 | this.name = name; 44 | } 45 | 46 | @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY) 47 | @JoinTable( name = "USER_GROUP", 48 | joinColumns = @JoinColumn(name = "USER_ID", referencedColumnName="ID"), 49 | inverseJoinColumns = @JoinColumn(name = "GROUP_ID", referencedColumnName="ID")) 50 | public Set getGroups() { 51 | return groups; 52 | } 53 | 54 | public void setGroups(Set groups) { 55 | this.groups = groups; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyunidirectional/mapper/GroupMapper.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyunidirectional.mapper; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import com.oodd.spring.manytomanyunidirectional.dto.GroupDto; 6 | import com.oodd.spring.manytomanyunidirectional.entity.Group; 7 | 8 | @Component 9 | public class GroupMapper { 10 | 11 | public Group mapDtoToEntity(GroupDto groupDto) { 12 | Group group = new Group(); 13 | if(null != groupDto.getId()) group.setId(groupDto.getId()); 14 | if(null != groupDto.getName()) group.setName(groupDto.getName()); 15 | return group; 16 | } 17 | 18 | public GroupDto mapEntityToDto(Group group) { 19 | GroupDto groupDto = new GroupDto(); 20 | if(null != group.getId()) groupDto.setId(group.getId()); 21 | if(null != group.getName()) groupDto.setName(group.getName()); 22 | return groupDto; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyunidirectional/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyunidirectional.mapper; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import com.oodd.spring.manytomanyunidirectional.dto.UserDto; 6 | import com.oodd.spring.manytomanyunidirectional.entity.User; 7 | 8 | @Component 9 | public class UserMapper { 10 | 11 | public User mapDtoToEntity(UserDto userDto) { 12 | User user = new User(); 13 | if(null != userDto.getId()) user.setId(userDto.getId()); 14 | if(null != userDto.getName()) user.setName(userDto.getName()); 15 | return user; 16 | } 17 | 18 | public UserDto mapEntityToDto(User user) { 19 | UserDto userDto = new UserDto(); 20 | if(null != user.getId()) userDto.setId(user.getId()); 21 | if(null != user.getName()) userDto.setName(user.getName()); 22 | return userDto; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyunidirectional/mock/UserGroupInMemoryDB.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyunidirectional.mock; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.oodd.spring.manytomanyunidirectional.dto.UserGroupDto; 7 | 8 | public enum UserGroupInMemoryDB { 9 | 10 | INSTANCE; 11 | 12 | private static List list = new ArrayList(); 13 | 14 | public void add(UserGroupDto userGroupDto) { 15 | list.add(userGroupDto); 16 | } 17 | 18 | public boolean isPresent(UserGroupDto userGroupDto) { 19 | for (UserGroupDto dto:list) { 20 | if (dto.getUserDto().getId() == userGroupDto.getUserDto().getId() 21 | && dto.getGroupDto().getId() == userGroupDto.getGroupDto().getId()) { 22 | return true; 23 | } 24 | } 25 | return false; 26 | } 27 | 28 | public void remove(UserGroupDto userGroupDto) { 29 | UserGroupDto toRemove = null; 30 | for (UserGroupDto dto:list) { 31 | if (dto.getUserDto().getId()==userGroupDto.getUserDto().getId() 32 | && dto.getGroupDto().getId()==userGroupDto.getGroupDto().getId()) { 33 | toRemove = dto; 34 | } 35 | } 36 | if (toRemove!=null) list.remove(toRemove); 37 | } 38 | 39 | public List findAll() { 40 | return list; 41 | } 42 | 43 | public void setList(List list) { 44 | UserGroupInMemoryDB.list = list; 45 | } 46 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyunidirectional/mock/UserGroupMockController.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyunidirectional.mock; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.http.HttpStatus; 6 | import org.springframework.stereotype.Controller; 7 | import org.springframework.web.bind.annotation.RequestBody; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RequestMethod; 10 | import org.springframework.web.bind.annotation.ResponseBody; 11 | import org.springframework.web.bind.annotation.ResponseStatus; 12 | 13 | import com.oodd.spring.manytomanyunidirectional.dto.UserGroupDto; 14 | 15 | @Controller 16 | @RequestMapping(value="/manytomanyunidirectional/usergroup/mock") 17 | public class UserGroupMockController { 18 | 19 | @RequestMapping(value="/findAll", method=RequestMethod.GET) 20 | public @ResponseBody List findAll(){ 21 | return UserGroupInMemoryDB.INSTANCE.findAll(); 22 | } 23 | @RequestMapping(value="/create", method=RequestMethod.POST) 24 | @ResponseBody 25 | public void create(@RequestBody UserGroupDto userGroupDto){ 26 | UserGroupInMemoryDB.INSTANCE.add(userGroupDto); 27 | } 28 | @RequestMapping(value="/remove", method=RequestMethod.POST) 29 | @ResponseStatus(value = HttpStatus.NO_CONTENT) 30 | public void remove(@RequestBody UserGroupDto userGroupDto){ 31 | UserGroupInMemoryDB.INSTANCE.remove(userGroupDto); 32 | } 33 | @RequestMapping(value="/isPresent", method=RequestMethod.POST) 34 | public @ResponseBody boolean isPresent(@RequestBody UserGroupDto userGroupDto){ 35 | return UserGroupInMemoryDB.INSTANCE.isPresent(userGroupDto); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyunidirectional/service/GroupService.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyunidirectional.service; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanyunidirectional.dto.GroupDto; 6 | 7 | public interface GroupService { 8 | 9 | public void create(GroupDto groupDto ) ; 10 | public List findAll(); 11 | public GroupDto findById(Integer id); 12 | public void remove(Integer groupId); 13 | public void edit(GroupDto groupDto); 14 | } 15 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyunidirectional/service/UserGroupService.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyunidirectional.service; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanyunidirectional.dto.UserGroupDto; 6 | 7 | public interface UserGroupService { 8 | 9 | public boolean isPresent(UserGroupDto userGroupDto); 10 | public List findAll(); 11 | public void create(UserGroupDto userGroupDto); 12 | public void remove(UserGroupDto userGroupDto); 13 | } 14 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/manytomanyunidirectional/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyunidirectional.service; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.manytomanyunidirectional.dto.UserDto; 6 | 7 | public interface UserService { 8 | 9 | public void create(UserDto userDto ) ; 10 | public List findAll(); 11 | public UserDto findById(Integer id); 12 | public void remove(Integer userId); 13 | public void edit(UserDto userDto); 14 | } 15 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetomanybidirectional/dao/ItemDao.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetomanybidirectional.dao; 2 | import java.util.List; 3 | import com.oodd.spring.onetomanybidirectional.entity.Item; 4 | public interface ItemDao { 5 | public void insert(Item item); 6 | public List getAll(); 7 | public Item getById(Integer id); 8 | public void delete(Item item); 9 | public void update(Item item); 10 | } 11 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetomanybidirectional/dao/impl/ItemDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetomanybidirectional.dao.impl; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Repository; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import com.oodd.spring.onetomanybidirectional.dao.ItemDao; 11 | import com.oodd.spring.onetomanybidirectional.entity.Item; 12 | @Repository 13 | @Transactional 14 | public class ItemDaoImpl implements ItemDao { 15 | @Autowired 16 | private SessionFactory sessionFactory ; 17 | @Override 18 | public void insert(Item item) { 19 | sessionFactory.getCurrentSession().save(item); 20 | } 21 | @SuppressWarnings("unchecked") 22 | @Override 23 | public List getAll() { 24 | return (List)sessionFactory.getCurrentSession(). 25 | createQuery("select item from Item item order by item.id desc ").list(); 26 | } 27 | @Override 28 | public Item getById(Integer id) { 29 | return (Item) sessionFactory.getCurrentSession().get(Item.class, id); 30 | } 31 | @Override 32 | public void delete(Item item) { 33 | sessionFactory.getCurrentSession().delete(item); 34 | } 35 | @Override 36 | public void update(Item item) { 37 | sessionFactory.getCurrentSession().merge(item); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetomanybidirectional/dto/ItemDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetomanybidirectional.dto; 2 | 3 | import java.util.List; 4 | 5 | public class ItemDto { 6 | private Integer id; 7 | private String name; 8 | private List featureList; 9 | 10 | public Integer getId() { 11 | return id; 12 | } 13 | public void setId(Integer id) { 14 | this.id = id; 15 | } 16 | public String getName() { 17 | return name; 18 | } 19 | public void setName(String name) { 20 | this.name = name; 21 | } 22 | public List getFeatureList() { 23 | return featureList; 24 | } 25 | public void setFeatureList(List featureList) { 26 | this.featureList = featureList; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetomanybidirectional/entity/Feature.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetomanybidirectional.entity; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Id; 8 | import javax.persistence.JoinColumn; 9 | import javax.persistence.ManyToOne; 10 | import javax.persistence.Table; 11 | 12 | @Entity 13 | @Table(name="FEATURE") 14 | public class Feature { 15 | private Integer id; 16 | private String name; 17 | private Item item; 18 | @Id 19 | @GeneratedValue(strategy=GenerationType.AUTO) 20 | @Column(name="ID",length=20) 21 | public Integer getId() { 22 | return id; 23 | } 24 | public void setId(Integer id) { 25 | this.id = id; 26 | } 27 | @Column(name="name",length=50) 28 | public String getName() { 29 | return name; 30 | } 31 | public void setName(String name) { 32 | this.name = name; 33 | } 34 | @ManyToOne 35 | @JoinColumn(name="ITEM_ID", nullable=false) 36 | public Item getItem() { 37 | return item; 38 | } 39 | public void setItem(Item item) { 40 | this.item = item; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetomanybidirectional/entity/Item.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetomanybidirectional.entity; 2 | 3 | import java.util.List; 4 | 5 | import javax.persistence.CascadeType; 6 | import javax.persistence.Column; 7 | import javax.persistence.Entity; 8 | import javax.persistence.GeneratedValue; 9 | import javax.persistence.GenerationType; 10 | import javax.persistence.Id; 11 | import javax.persistence.OneToMany; 12 | import javax.persistence.Table; 13 | 14 | @Entity 15 | @Table(name="ITEM") 16 | public class Item { 17 | private Integer id; 18 | private String name; 19 | private List features; 20 | @Id 21 | @GeneratedValue(strategy=GenerationType.AUTO) 22 | @Column(name="ID",length=20) 23 | public Integer getId() { 24 | return id; 25 | } 26 | public void setId(Integer id) { 27 | this.id = id; 28 | } 29 | @Column(name="name",length=50) 30 | public String getName() { 31 | return name; 32 | } 33 | public void setName(String name) { 34 | this.name = name; 35 | } 36 | @OneToMany(mappedBy="item",cascade=CascadeType.ALL,orphanRemoval=true) 37 | public List getFeatures() { 38 | return features; 39 | } 40 | public void setFeatures(List features) { 41 | this.features = features; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetomanybidirectional/mapper/ItemMapper.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetomanybidirectional.mapper; 2 | import java.util.ArrayList; 3 | import java.util.List; 4 | 5 | import org.springframework.stereotype.Component; 6 | 7 | import com.oodd.spring.onetomanybidirectional.dto.ItemDto; 8 | import com.oodd.spring.onetomanybidirectional.entity.Feature; 9 | import com.oodd.spring.onetomanybidirectional.entity.Item; 10 | @Component 11 | public class ItemMapper { 12 | public Item mapDtoToEntity(ItemDto itemDto){ 13 | Item item = new Item(); 14 | if(null != itemDto.getId()){item.setId(itemDto.getId());} 15 | if(null != itemDto.getName()){item.setName(itemDto.getName());} 16 | if(null != itemDto.getFeatureList() && itemDto.getFeatureList().size()>0){ 17 | List features = new ArrayList(); 18 | for(String feature : itemDto.getFeatureList()){ 19 | Feature featureObject = new Feature(); 20 | featureObject.setName(feature); 21 | featureObject.setItem(item); 22 | features.add(featureObject); 23 | } 24 | item.setFeatures(features); 25 | } 26 | return item ; 27 | } 28 | public ItemDto mapEntityToDto(Item item){ 29 | ItemDto itemDto = new ItemDto(); 30 | if(null != item.getId()){itemDto.setId(item.getId());} 31 | if(null != item.getName()){itemDto.setName(item.getName());} 32 | if(null != item.getFeatures() && item.getFeatures().size() > 0){ 33 | List featuers = new ArrayList(); 34 | for(Feature feature : item.getFeatures()){ 35 | featuers.add(feature.getName()); 36 | } 37 | itemDto.setFeatureList(featuers); 38 | } 39 | return itemDto; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetomanybidirectional/mock/ItemInMemoryDB.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetomanybidirectional.mock; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.oodd.spring.onetomanybidirectional.dto.ItemDto; 7 | 8 | public enum ItemInMemoryDB { 9 | INSTANCE; 10 | private static List list = new ArrayList(); 11 | private static Integer lastId = 0; 12 | public Integer getId() { 13 | return ++lastId; 14 | } 15 | public void add(ItemDto itemDto){ 16 | itemDto.setId(getId()); 17 | list.add(itemDto); 18 | } 19 | public void edit(ItemDto itemDto){ 20 | for(ItemDto dto : list){ 21 | if(dto.getId() == itemDto.getId()){ 22 | dto.setName(itemDto.getName()); 23 | dto.setFeatureList(itemDto.getFeatureList()); 24 | } 25 | } 26 | } 27 | public void remove(Integer id) { 28 | ItemDto dto = null; 29 | for(ItemDto itemDto : list){ 30 | if(itemDto.getId() == id){ 31 | dto = itemDto; 32 | break; 33 | } 34 | } 35 | if(null != dto){list.remove(dto);} 36 | } 37 | public List findAll(){ 38 | return list; 39 | } 40 | public ItemDto findById(Integer id) { 41 | for(ItemDto itemDto : list){ 42 | if(itemDto.getId() == id) 43 | return itemDto ; 44 | } 45 | return null; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetomanybidirectional/service/ItemService.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetomanybidirectional.service; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.onetomanybidirectional.dto.ItemDto; 6 | 7 | public interface ItemService { 8 | public void create(ItemDto itemDto); 9 | public List findAll(); 10 | public ItemDto findById(Integer id); 11 | public void remove(Integer id); 12 | public void edit(ItemDto itemDto); 13 | } 14 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetomanybidirectional/service/impl/ItemServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetomanybidirectional.service.impl; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import com.oodd.spring.onetomanybidirectional.dao.ItemDao; 11 | import com.oodd.spring.onetomanybidirectional.dto.ItemDto; 12 | import com.oodd.spring.onetomanybidirectional.entity.Item; 13 | import com.oodd.spring.onetomanybidirectional.mapper.ItemMapper; 14 | import com.oodd.spring.onetomanybidirectional.service.ItemService; 15 | @Service 16 | @Transactional 17 | public class ItemServiceImpl implements ItemService{ 18 | @Autowired 19 | private ItemDao dao; 20 | @Autowired 21 | private ItemMapper mapper; 22 | @Override 23 | public void create(ItemDto itemDto) { 24 | dao.insert(mapper.mapDtoToEntity(itemDto)); 25 | } 26 | @Override 27 | public List findAll() { 28 | List items = dao.getAll(); 29 | List itemDtos = new ArrayList(); 30 | if(null !=items){ 31 | for (Item item : items) { 32 | itemDtos.add(mapper.mapEntityToDto(item)); 33 | } 34 | } 35 | return itemDtos; 36 | } 37 | @Override 38 | public ItemDto findById(Integer id) { 39 | Item item = dao.getById(id); 40 | if(null !=item){ 41 | return mapper.mapEntityToDto(item); 42 | } 43 | return null; 44 | } 45 | @Override 46 | public void remove(Integer id) { 47 | Item item = dao.getById(id); 48 | if(null != item){ 49 | dao.delete(item); 50 | } 51 | } 52 | @Override 53 | public void edit(ItemDto itemDto) { 54 | dao.update(mapper.mapDtoToEntity(itemDto)); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetomanyselfreference/dao/CategoryDao.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetomanyselfreference.dao; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.onetomanyselfreference.entity.Category; 6 | 7 | public interface CategoryDao { 8 | public void insert(Category category); 9 | public Category getById(Integer id); 10 | public void delete(Category category); 11 | public void update(Category category); 12 | public List getAll(); 13 | } 14 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetomanyselfreference/dao/impl/CategoryDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetomanyselfreference.dao.impl; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Repository; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import com.oodd.spring.onetomanyselfreference.dao.CategoryDao; 11 | import com.oodd.spring.onetomanyselfreference.entity.Category; 12 | @Repository 13 | @Transactional 14 | public class CategoryDaoImpl implements CategoryDao { 15 | @Autowired 16 | private SessionFactory sessionFactory; 17 | 18 | @Override 19 | public void insert(Category category) { 20 | sessionFactory.getCurrentSession().save(category); 21 | } 22 | 23 | @Override 24 | public Category getById(Integer id) { 25 | return (Category) sessionFactory.getCurrentSession().get(Category.class, id); 26 | } 27 | 28 | @Override 29 | public void delete(Category category) { 30 | sessionFactory.getCurrentSession().delete(category); 31 | } 32 | 33 | @Override 34 | public void update(Category category) { 35 | sessionFactory.getCurrentSession().merge(category); 36 | } 37 | 38 | @SuppressWarnings("unchecked") 39 | @Override 40 | public List getAll() { 41 | return sessionFactory.getCurrentSession().createQuery("select category from Category category order by id desc").list(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetomanyselfreference/dto/CategoryDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetomanyselfreference.dto; 2 | 3 | public class CategoryDto { 4 | private Integer id; 5 | private String name; 6 | private Integer parentId; 7 | public Integer getId() { 8 | return id; 9 | } 10 | public void setId(Integer id) { 11 | this.id = id; 12 | } 13 | public String getName() { 14 | return name; 15 | } 16 | public void setName(String name) { 17 | this.name = name; 18 | } 19 | public Integer getParentId() { 20 | return parentId; 21 | } 22 | public void setParentId(Integer parentId) { 23 | this.parentId = parentId; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetomanyselfreference/entity/Category.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetomanyselfreference.entity; 2 | 3 | import javax.persistence.CascadeType; 4 | import javax.persistence.Column; 5 | import javax.persistence.Entity; 6 | import javax.persistence.GeneratedValue; 7 | import javax.persistence.GenerationType; 8 | import javax.persistence.Id; 9 | import javax.persistence.JoinColumn; 10 | import javax.persistence.ManyToOne; 11 | import javax.persistence.Table; 12 | 13 | @Entity 14 | @Table(name="CATEGORY") 15 | public class Category { 16 | private Integer id; 17 | private String name; 18 | private Category category ; 19 | @Id 20 | @GeneratedValue(strategy=GenerationType.AUTO) 21 | @Column(name="id",length=20) 22 | public Integer getId() { 23 | return id; 24 | } 25 | public void setId(Integer id) { 26 | this.id = id; 27 | } 28 | @Column(name="name",length=20) 29 | public String getName() { 30 | return name; 31 | } 32 | public void setName(String name) { 33 | this.name = name; 34 | } 35 | @ManyToOne(cascade=CascadeType.PERSIST) 36 | @JoinColumn(name="CAT_ID",nullable=true) 37 | public Category getCategory() { 38 | return category; 39 | } 40 | public void setCategory(Category category) { 41 | this.category = category; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetomanyselfreference/mapper/CategoryMapper.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetomanyselfreference.mapper; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import com.oodd.spring.onetomanyselfreference.dto.CategoryDto; 6 | import com.oodd.spring.onetomanyselfreference.entity.Category; 7 | 8 | @Component 9 | public class CategoryMapper { 10 | public Category mapDtoToEntity(CategoryDto categoryDto){ 11 | Category category = new Category(); 12 | if(null !=categoryDto.getId()) category.setId(categoryDto.getId()); 13 | if(null !=categoryDto.getName()) category.setName(categoryDto.getName()); 14 | return category; 15 | } 16 | public CategoryDto mapEntityToDto(Category category){ 17 | CategoryDto categoryDto = new CategoryDto(); 18 | if(null !=category.getId()) categoryDto.setId(category.getId()); 19 | if(null !=category.getName()) categoryDto.setName(category.getName()); 20 | if(null !=category.getCategory()) categoryDto.setParentId(category.getCategory().getId()); 21 | return categoryDto; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetomanyselfreference/mock/CategoryInMemoryDB.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetomanyselfreference.mock; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.oodd.spring.onetomanyselfreference.dto.CategoryDto; 7 | public enum CategoryInMemoryDB { 8 | INSTANCE; 9 | 10 | private static List list = new ArrayList(); 11 | 12 | private static Integer lastId = 0; 13 | 14 | public Integer getId() { 15 | return ++lastId; 16 | } 17 | public void add(CategoryDto categoryDto){ 18 | categoryDto.setId(getId()); 19 | list.add(categoryDto); 20 | } 21 | public void edit(CategoryDto categoryDto){ 22 | for(CategoryDto dto : list){ 23 | if(categoryDto.getId() == dto.getId()){ 24 | dto.setName(categoryDto.getName()); 25 | dto.setParentId(categoryDto.getParentId()); 26 | break; 27 | } 28 | } 29 | } 30 | public void remove(Integer id) { 31 | CategoryDto dto = null; 32 | for(CategoryDto categoryDto : list){ 33 | if(categoryDto.getId() == id) { 34 | boolean flag = false; 35 | for(CategoryDto categoryDto2 : list){ 36 | if (id==categoryDto2.getParentId()) 37 | flag = true; 38 | } 39 | if (!flag) { 40 | dto = categoryDto; 41 | break; 42 | } 43 | } 44 | } 45 | if(null != dto) list.remove(dto); 46 | } 47 | public List findAll(){ return list; } 48 | public CategoryDto findById(Integer id) { 49 | for(CategoryDto categoryDto : list){ 50 | if(categoryDto.getId() == id) 51 | return categoryDto; 52 | } 53 | return null ; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetomanyselfreference/service/CategoryService.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetomanyselfreference.service; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.onetomanyselfreference.dto.CategoryDto; 6 | 7 | public interface CategoryService { 8 | public void create(CategoryDto categoryDto); 9 | public CategoryDto findById(Integer id); 10 | public void remove(Integer id); 11 | public void edit(CategoryDto categoryDto); 12 | public List findAll(); 13 | } 14 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetomanyunidirectional/dao/PersonDao.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetomanyunidirectional.dao; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.onetomanyunidirectional.entity.Person; 6 | 7 | public interface PersonDao { 8 | public void insert(Person person); 9 | public List getAll(); 10 | public Person getById(Integer id); 11 | public void delete(Person person) ; 12 | public void update(Person person); 13 | } 14 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetomanyunidirectional/dao/impl/PersonDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetomanyunidirectional.dao.impl; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Repository; 8 | import org.springframework.transaction.annotation.Propagation; 9 | import org.springframework.transaction.annotation.Transactional; 10 | 11 | import com.oodd.spring.onetomanyunidirectional.dao.PersonDao; 12 | import com.oodd.spring.onetomanyunidirectional.entity.Person; 13 | import com.oodd.spring.onetomanyunidirectional.entity.Phone; 14 | @Repository 15 | @Transactional 16 | public class PersonDaoImpl implements PersonDao{ 17 | @Autowired 18 | private SessionFactory sessionFactory ; 19 | 20 | @Override 21 | public void insert(Person person) { 22 | sessionFactory.getCurrentSession().save(person); 23 | } 24 | 25 | @SuppressWarnings("unchecked") 26 | @Override 27 | public List getAll() { 28 | return (List)sessionFactory.getCurrentSession(). 29 | createQuery("select person from Person person order by person.id desc").list(); 30 | } 31 | 32 | @Override 33 | public Person getById(Integer id) { 34 | return (Person)sessionFactory.getCurrentSession().get(Person.class,id); 35 | } 36 | 37 | @Override 38 | public void delete(Person person) { 39 | sessionFactory.getCurrentSession().delete(person); 40 | } 41 | 42 | @Override 43 | public void update(Person person) { 44 | sessionFactory.getCurrentSession().merge(person); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetomanyunidirectional/dto/PersonDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetomanyunidirectional.dto; 2 | 3 | import java.util.List; 4 | 5 | public class PersonDto { 6 | private Integer id; 7 | private String name; 8 | private List numbers; 9 | public Integer getId() { 10 | return id; 11 | } 12 | public void setId(Integer id) { 13 | this.id = id; 14 | } 15 | public String getName() { 16 | return name; 17 | } 18 | public void setName(String name) { 19 | this.name = name; 20 | } 21 | public List getNumbers() { 22 | return numbers; 23 | } 24 | public void setNumbers(List numbers) { 25 | this.numbers = numbers; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetomanyunidirectional/entity/Person.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetomanyunidirectional.entity; 2 | 3 | import java.util.List; 4 | 5 | import javax.persistence.CascadeType; 6 | import javax.persistence.Column; 7 | import javax.persistence.Entity; 8 | import javax.persistence.GeneratedValue; 9 | import javax.persistence.GenerationType; 10 | import javax.persistence.Id; 11 | import javax.persistence.JoinColumn; 12 | import javax.persistence.OneToMany; 13 | import javax.persistence.Table; 14 | @Entity 15 | @Table(name="PERSON") 16 | public class Person { 17 | private Integer id; 18 | private String name; 19 | private List phones ; 20 | @Id 21 | @GeneratedValue(strategy=GenerationType.AUTO) 22 | @Column(name="ID",length=20) 23 | public Integer getId() { 24 | return id; 25 | } 26 | public void setId(Integer id) { 27 | this.id = id; 28 | } 29 | @Column(name="name",length=50) 30 | public String getName() { 31 | return name; 32 | } 33 | public void setName(String name) { 34 | this.name = name; 35 | } 36 | @OneToMany(cascade=CascadeType.ALL,orphanRemoval=true) 37 | @JoinColumn(name="PERSON_ID",referencedColumnName="ID") 38 | public List getPhones() { 39 | return phones; 40 | } 41 | public void setPhones(List phones) { 42 | this.phones = phones; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetomanyunidirectional/entity/Phone.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetomanyunidirectional.entity; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Id; 8 | import javax.persistence.Table; 9 | 10 | @Entity 11 | @Table(name="PHONE") 12 | public class Phone { 13 | private Integer id; 14 | private String number; 15 | @Id 16 | @GeneratedValue(strategy=GenerationType.AUTO) 17 | @Column(name="ID",length=20) 18 | public Integer getId() { 19 | return id; 20 | } 21 | public void setId(Integer id) { 22 | this.id = id; 23 | } 24 | @Column(name="NUMBER",length=20) 25 | public String getNumber() { 26 | return number; 27 | } 28 | public void setNumber(String number) { 29 | this.number = number; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetomanyunidirectional/mapper/PersonMapper.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetomanyunidirectional.mapper; 2 | import java.util.ArrayList; 3 | import java.util.List; 4 | 5 | import org.springframework.stereotype.Component; 6 | 7 | import com.oodd.spring.onetomanyunidirectional.dto.PersonDto; 8 | import com.oodd.spring.onetomanyunidirectional.entity.Person; 9 | import com.oodd.spring.onetomanyunidirectional.entity.Phone; 10 | @Component 11 | public class PersonMapper { 12 | public Person mapDtoToEntity(PersonDto personDto){ 13 | Person person = new Person(); 14 | if(null !=personDto.getId()){person.setId(personDto.getId());} 15 | if(null !=personDto.getName()){person.setName(personDto.getName());} 16 | if(null !=personDto.getNumbers()&& personDto.getNumbers().size() > 0){ 17 | List phoneList = new ArrayList(); 18 | if(personDto.getNumbers()!=null && personDto.getNumbers().size()>0){ 19 | for(String number : personDto.getNumbers()){ 20 | Phone phone = new Phone(); 21 | phone.setNumber(number); 22 | phoneList.add(phone);} 23 | } 24 | person.setPhones(phoneList); 25 | } 26 | return person; 27 | } 28 | public PersonDto mapEntityToDto(Person person){ 29 | PersonDto personDto = new PersonDto(); 30 | if(null !=person.getId()){personDto.setId(person.getId());} 31 | if(null !=person.getName()){personDto.setName(person.getName());} 32 | if(null !=person.getPhones() && person.getPhones().size() >0){ 33 | List phones = new ArrayList(); 34 | for(Phone phone : person.getPhones()){phones.add(phone.getNumber());} 35 | personDto.setNumbers(phones); 36 | } 37 | return personDto; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetomanyunidirectional/mock/PersonInMemoryDB.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetomanyunidirectional.mock; 2 | import java.util.ArrayList; 3 | import java.util.List; 4 | import com.oodd.spring.onetomanyunidirectional.dto.PersonDto; 5 | public enum PersonInMemoryDB { 6 | INSTANCE; 7 | private static Integer lastId = 0; 8 | private static List list = new ArrayList(); 9 | public Integer getId() { 10 | return ++lastId; 11 | } 12 | public void add(PersonDto personDto){ 13 | personDto.setId(getId()); 14 | list.add(personDto); 15 | } 16 | public void edit(PersonDto personDto){ 17 | for(PersonDto dto : list){ 18 | if(dto.getId() == personDto.getId()){ 19 | dto.setName(personDto.getName()); 20 | dto.setNumbers(personDto.getNumbers()); 21 | } 22 | } 23 | } 24 | public void remove(Integer id) { 25 | PersonDto dto = null; 26 | for(PersonDto personDto : list){ 27 | if(personDto.getId() == id){dto = personDto;break;} 28 | } 29 | if(null !=dto){list.remove(dto);} 30 | } 31 | public List findAll(){ 32 | return list; 33 | } 34 | public PersonDto findById(Integer id) { 35 | for(PersonDto personDto : list){ 36 | if(personDto.getId() == id) return personDto ; 37 | } 38 | return null; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetomanyunidirectional/service/PersonService.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetomanyunidirectional.service; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.onetomanyunidirectional.dto.PersonDto; 6 | 7 | public interface PersonService { 8 | public void create(PersonDto personDto) ; 9 | public List findAll() ; 10 | public PersonDto findById(Integer id) ; 11 | public void remove(Integer id); 12 | public void edit(PersonDto personDto) ; 13 | } 14 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetoonebidirectional/dao/CustomerDao.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetoonebidirectional.dao; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.onetoonebidirectional.entity.Customer; 6 | 7 | public interface CustomerDao { 8 | public void insert(Customer customer); 9 | public List getAll(); 10 | public Customer getById(int id); 11 | public void delete(Customer customer); 12 | public void update(Customer customer); 13 | } 14 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetoonebidirectional/dao/impl/CustomerDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetoonebidirectional.dao.impl; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Repository; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import com.oodd.spring.onetoonebidirectional.dao.CustomerDao; 11 | import com.oodd.spring.onetoonebidirectional.entity.Customer; 12 | 13 | @Repository 14 | @Transactional 15 | public class CustomerDaoImpl implements CustomerDao{ 16 | @Autowired 17 | private SessionFactory sessionFactory ; 18 | @Override 19 | public void insert(Customer customer) { 20 | sessionFactory.getCurrentSession().save(customer); 21 | } 22 | 23 | @SuppressWarnings("unchecked") 24 | @Override 25 | public List getAll() { 26 | return sessionFactory.getCurrentSession(). 27 | createQuery("select customer from Customer customer order by customer.id desc").list(); 28 | } 29 | 30 | @Override 31 | public Customer getById(int id) { 32 | return (Customer) sessionFactory.getCurrentSession().get(Customer.class,id); 33 | } 34 | 35 | @Override 36 | public void delete(Customer customer) { 37 | sessionFactory.getCurrentSession().delete(customer); 38 | } 39 | 40 | @Override 41 | public void update(Customer customer) { 42 | sessionFactory.getCurrentSession().merge(customer); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetoonebidirectional/dto/CustomerDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetoonebidirectional.dto; 2 | 3 | public class CustomerDto { 4 | private Integer id; 5 | private String name; 6 | private double amount; 7 | public Integer getId() { 8 | return id; 9 | } 10 | public void setId(Integer id) { 11 | this.id = id; 12 | } 13 | public String getName() { 14 | return name; 15 | } 16 | public void setName(String name) { 17 | this.name = name; 18 | } 19 | public double getAmount() { 20 | return amount; 21 | } 22 | public void setAmount(double amount) { 23 | this.amount = amount; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetoonebidirectional/entity/Cart.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetoonebidirectional.entity; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Id; 8 | import javax.persistence.OneToOne; 9 | import javax.persistence.Table; 10 | @Entity 11 | @Table(name="CART") 12 | public class Cart { 13 | private Integer id; 14 | private Double amount; 15 | private Customer customer ; 16 | @Id 17 | @GeneratedValue(strategy=GenerationType.AUTO) 18 | @Column(name="id",length=20) 19 | public Integer getId() { 20 | return id; 21 | } 22 | public void setId(Integer id) { 23 | this.id = id; 24 | } 25 | @Column(name="amount",length=20) 26 | public Double getAmount() { 27 | return amount; 28 | } 29 | public void setAmount(Double amount) { 30 | this.amount = amount; 31 | } 32 | @OneToOne(mappedBy="cart") 33 | public Customer getCustomer() { 34 | return customer; 35 | } 36 | public void setCustomer(Customer customer) { 37 | this.customer = customer; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetoonebidirectional/entity/Customer.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetoonebidirectional.entity; 2 | 3 | import javax.persistence.CascadeType; 4 | import javax.persistence.Column; 5 | import javax.persistence.Entity; 6 | import javax.persistence.GeneratedValue; 7 | import javax.persistence.GenerationType; 8 | import javax.persistence.Id; 9 | import javax.persistence.JoinColumn; 10 | import javax.persistence.OneToOne; 11 | import javax.persistence.Table; 12 | 13 | @Entity 14 | @Table(name="CUSTOMER") 15 | public class Customer { 16 | private Integer id ; 17 | private String name; 18 | private Cart cart ; 19 | @Id 20 | @GeneratedValue(strategy=GenerationType.AUTO) 21 | @Column(name="id",length=20) 22 | public Integer getId() { 23 | return id; 24 | } 25 | public void setId(Integer id) { 26 | this.id = id; 27 | } 28 | @Column(name="name",length=50) 29 | public String getName() { 30 | return name; 31 | } 32 | public void setName(String name) { 33 | this.name = name; 34 | } 35 | @OneToOne(cascade=CascadeType.ALL, orphanRemoval=true) 36 | @JoinColumn(name="CART_ID") 37 | public Cart getCart() { 38 | return cart; 39 | } 40 | public void setCart(Cart cart) { 41 | this.cart = cart; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetoonebidirectional/mapper/CustomerMapper.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetoonebidirectional.mapper; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import com.oodd.spring.onetoonebidirectional.dto.CustomerDto; 6 | import com.oodd.spring.onetoonebidirectional.entity.Cart; 7 | import com.oodd.spring.onetoonebidirectional.entity.Customer; 8 | @Component 9 | public class CustomerMapper { 10 | public Customer mapDtoToEntity(CustomerDto customerDto){ 11 | Customer customer = new Customer(); 12 | Cart cart = new Cart(); 13 | if(null!=customerDto.getId())customer.setId(customerDto.getId()); 14 | if(null!=customerDto.getName())customer.setName(customerDto.getName()); 15 | if(customerDto.getAmount()>0){ 16 | cart.setAmount(customerDto.getAmount()); 17 | cart.setCustomer(customer); 18 | customer.setCart(cart); 19 | } 20 | return customer; 21 | } 22 | public CustomerDto mapEntityToDto(Customer customer){ 23 | CustomerDto customerDto = new CustomerDto() ; 24 | if(null!=customer.getId())customerDto.setId(customer.getId()); 25 | if(null!=customer.getName())customerDto.setName(customer.getName()); 26 | if(null!=customer.getCart()){ 27 | if(0!=customer.getCart().getAmount()) 28 | customerDto.setAmount(customer.getCart().getAmount()); 29 | } 30 | return customerDto; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetoonebidirectional/mock/CustomerInMemoryDB.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetoonebidirectional.mock; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.oodd.spring.onetoonebidirectional.dto.CustomerDto; 7 | 8 | public enum CustomerInMemoryDB { 9 | INSTANCE; 10 | private static Integer lastId = 0; 11 | private static List list = new ArrayList(); 12 | public Integer getId() { 13 | return ++lastId; 14 | } 15 | public void add(CustomerDto customerDto){ 16 | customerDto.setId(getId()); 17 | list.add(customerDto); 18 | } 19 | public void edit(CustomerDto customerDto){ 20 | for(CustomerDto dto : list){ 21 | if(dto.getId() == customerDto.getId()){ 22 | dto.setName(customerDto.getName()); 23 | dto.setAmount(customerDto.getAmount()); 24 | } 25 | } 26 | } 27 | public void remove(Integer id) { 28 | CustomerDto dto = null; 29 | for(CustomerDto customerDto : list){ 30 | if(customerDto.getId() == id){dto = customerDto;break;} 31 | } 32 | if(null !=dto){list.remove(dto);} 33 | } 34 | public List findAll(){ 35 | return list; 36 | } 37 | public CustomerDto findById(Integer id) { 38 | for(CustomerDto customerDto : list){ 39 | if(customerDto.getId() == id) return customerDto ; 40 | } 41 | return null; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetoonebidirectional/service/CustomerService.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetoonebidirectional.service; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.onetoonebidirectional.dto.CustomerDto; 6 | 7 | public interface CustomerService { 8 | public void create(CustomerDto customerDto); 9 | public List findAll(); 10 | public CustomerDto findById(int id) ; 11 | public void remove(int id); 12 | public void edit(CustomerDto customerDto); 13 | } 14 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetooneselfreference/dao/StudentDao.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetooneselfreference.dao; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.onetooneselfreference.entity.Student; 6 | 7 | public interface StudentDao { 8 | public void insert(Student student); 9 | public List getAll(); 10 | public Student getById(Integer id); 11 | public void delete(Student student) ; 12 | public void update(Student student); 13 | } 14 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetooneselfreference/dao/impl/StudentDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetooneselfreference.dao.impl; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Repository; 8 | import org.springframework.transaction.annotation.Propagation; 9 | import org.springframework.transaction.annotation.Transactional; 10 | 11 | import com.oodd.spring.onetooneselfreference.dao.StudentDao; 12 | import com.oodd.spring.onetooneselfreference.entity.Student; 13 | @Repository 14 | @Transactional 15 | public class StudentDaoImpl implements StudentDao{ 16 | @Autowired 17 | private SessionFactory sessionFactory ; 18 | 19 | @Override 20 | public void insert(Student student) { 21 | sessionFactory.getCurrentSession().save(student); 22 | } 23 | 24 | @SuppressWarnings("unchecked") 25 | @Override 26 | public List getAll() { 27 | return (List)sessionFactory.getCurrentSession(). 28 | createQuery("select student from Student student").list(); 29 | } 30 | 31 | @Override 32 | public Student getById(Integer id) { 33 | return (Student)sessionFactory.getCurrentSession().get(Student.class, id); 34 | } 35 | 36 | @Override 37 | public void delete(Student student) { 38 | sessionFactory.getCurrentSession().delete(student); 39 | } 40 | 41 | @Override 42 | public void update(Student student) { 43 | sessionFactory.getCurrentSession().merge(student); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetooneselfreference/dto/StudentDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetooneselfreference.dto; 2 | 3 | public class StudentDto { 4 | private Integer id; 5 | private String name; 6 | private String mentorName; 7 | public Integer getId() { 8 | return id; 9 | } 10 | public void setId(Integer id) { 11 | this.id = id; 12 | } 13 | public String getName() { 14 | return name; 15 | } 16 | public void setName(String name) { 17 | this.name = name; 18 | } 19 | public String getMentorName() { 20 | return mentorName; 21 | } 22 | public void setMentorName(String mentorName) { 23 | this.mentorName = mentorName; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetooneselfreference/entity/Student.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetooneselfreference.entity; 2 | 3 | import javax.persistence.CascadeType; 4 | import javax.persistence.Column; 5 | import javax.persistence.Entity; 6 | import javax.persistence.GeneratedValue; 7 | import javax.persistence.GenerationType; 8 | import javax.persistence.Id; 9 | import javax.persistence.JoinColumn; 10 | import javax.persistence.OneToOne; 11 | import javax.persistence.Table; 12 | 13 | import org.hibernate.annotations.Cascade; 14 | 15 | @Entity 16 | @Table(name="STUDENT") 17 | public class Student { 18 | private Integer id; 19 | private String name; 20 | private Student mentor; 21 | @Id 22 | @GeneratedValue(strategy=GenerationType.AUTO) 23 | @Column(name="ID",length=20) 24 | public Integer getId() { 25 | return id; 26 | } 27 | public void setId(int id) { 28 | this.id = id; 29 | } 30 | @Column(name="name",length=50) 31 | public String getName() { 32 | return name; 33 | } 34 | public void setName(String name) { 35 | this.name = name; 36 | } 37 | @OneToOne(cascade=CascadeType.ALL, orphanRemoval=true) 38 | @JoinColumn(name="mentor_id",nullable=true) 39 | public Student getMentor() { 40 | return mentor; 41 | } 42 | public void setMentor(Student mentor) { 43 | this.mentor = mentor; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetooneselfreference/mapper/StudentMapper.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetooneselfreference.mapper; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import com.oodd.spring.onetooneselfreference.dto.StudentDto; 6 | import com.oodd.spring.onetooneselfreference.entity.Student; 7 | @Component 8 | public class StudentMapper { 9 | public Student mapDtoToEntity (StudentDto studentDTO){ 10 | Student student = new Student(); 11 | Student mentor = new Student(); 12 | if(null !=studentDTO.getId() && studentDTO.getId()> 0) student.setId(studentDTO.getId()); 13 | if(null !=studentDTO.getName()) student.setName(studentDTO.getName()); 14 | if(null !=studentDTO.getMentorName()){mentor.setName(studentDTO.getMentorName());student.setMentor(mentor);} 15 | return student; 16 | } 17 | public StudentDto mapEntityToDto (Student student){ 18 | StudentDto studentDTO = new StudentDto(); 19 | if(null !=student.getId()) studentDTO.setId(student.getId()); 20 | if(null !=student.getName()) studentDTO.setName(student.getName()); 21 | if(null !=student.getMentor())studentDTO.setMentorName(student.getMentor().getName()); 22 | return studentDTO; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetooneselfreference/mock/StudentInMemoryDB.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetooneselfreference.mock; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.oodd.spring.onetooneselfreference.dto.StudentDto; 7 | 8 | public enum StudentInMemoryDB { 9 | 10 | INSTANCE; 11 | private static Integer lastId = 0; 12 | private static List list = new ArrayList(); 13 | 14 | public Integer getId() {return ++lastId;} 15 | 16 | public void add(StudentDto studentDto){ 17 | studentDto.setId(getId()); 18 | list.add(studentDto); 19 | } 20 | 21 | public void edit(StudentDto studentDto){ 22 | for(StudentDto dto : list){ 23 | if(studentDto.getId() == dto.getId()){ 24 | dto.setName(studentDto.getName()); 25 | dto.setMentorName(studentDto.getMentorName()); 26 | break; 27 | } 28 | } 29 | } 30 | 31 | public void remove(Integer id) { 32 | StudentDto dto = null; 33 | for(StudentDto studentDto : list){ 34 | if(studentDto.getId() == id) { 35 | dto = studentDto; 36 | break; 37 | } 38 | } 39 | if(null != dto) list.remove(dto); 40 | } 41 | 42 | public List findAll(){return list;} 43 | 44 | public StudentDto findById(Integer id) { 45 | for(StudentDto studentDto : list){ 46 | if(studentDto.getId() == id) 47 | return studentDto; 48 | } 49 | return null ; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetooneselfreference/service/StudentService.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetooneselfreference.service; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.onetooneselfreference.dto.StudentDto; 6 | 7 | public interface StudentService { 8 | public void create(StudentDto studentDTO); 9 | public List findAll(); 10 | public StudentDto findById(Integer id) ; 11 | public void remove(Integer id); 12 | public void edit(StudentDto studentDTO); 13 | } 14 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetooneunidirectional/dao/BookDao.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetooneunidirectional.dao; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.onetooneunidirectional.entity.Book; 6 | public interface BookDao { 7 | public void insert( Book book ) ; 8 | public List getAll(); 9 | public Book getById(int id); 10 | public void delete(Book book) ; 11 | public void update(Book book); 12 | } 13 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetooneunidirectional/dao/impl/BookDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetooneunidirectional.dao.impl; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Repository; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import com.oodd.spring.onetooneunidirectional.dao.BookDao; 11 | import com.oodd.spring.onetooneunidirectional.entity.Book; 12 | 13 | @Repository 14 | @Transactional 15 | public class BookDaoImpl implements BookDao { 16 | @Autowired 17 | private SessionFactory sessionFactory ; 18 | @Override 19 | public void insert(Book book) { 20 | sessionFactory.getCurrentSession().save(book); 21 | } 22 | @SuppressWarnings("unchecked") 23 | @Override 24 | public List getAll() { 25 | return (List) sessionFactory.getCurrentSession() 26 | .createQuery("select book from Book book order by book.id desc").list(); 27 | } 28 | @Override 29 | public Book getById(int id) { 30 | return (Book) sessionFactory.getCurrentSession().get(Book.class,id); 31 | } 32 | 33 | @Override 34 | public void delete(Book book) { 35 | sessionFactory.getCurrentSession().delete(book); 36 | } 37 | 38 | @Override 39 | public void update(Book book) { 40 | sessionFactory.getCurrentSession().merge(book); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetooneunidirectional/dto/BookDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetooneunidirectional.dto; 2 | public class BookDto { 3 | private Integer id; 4 | private String name; 5 | private String city; 6 | public Integer getId() { 7 | return id; 8 | } 9 | public void setId(Integer id) { 10 | this.id = id; 11 | } 12 | public String getName() { 13 | return name; 14 | } 15 | public void setName(String name) { 16 | this.name = name; 17 | } 18 | public String getCity() { 19 | return city; 20 | } 21 | public void setCity(String city) { 22 | this.city = city; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetooneunidirectional/entity/Book.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetooneunidirectional.entity; 2 | 3 | import javax.persistence.CascadeType; 4 | import javax.persistence.Column; 5 | import javax.persistence.Entity; 6 | import javax.persistence.GeneratedValue; 7 | import javax.persistence.GenerationType; 8 | import javax.persistence.Id; 9 | import javax.persistence.JoinColumn; 10 | import javax.persistence.OneToOne; 11 | import javax.persistence.Table; 12 | 13 | @Entity 14 | @Table(name="BOOK") 15 | public class Book { 16 | private Integer id; 17 | private String name; 18 | private Shipping shipping; 19 | @Id 20 | @GeneratedValue(strategy=GenerationType.AUTO) 21 | @Column(name="id",length=20) 22 | public Integer getId() { 23 | return id; 24 | } 25 | public void setId(Integer id) { 26 | this.id = id; 27 | } 28 | @Column(name="name",length=50) 29 | public String getName() { 30 | return name; 31 | } 32 | public void setName(String name) { 33 | this.name = name; 34 | } 35 | @OneToOne(cascade=CascadeType.ALL, orphanRemoval=true) 36 | @JoinColumn(name="shipping_id") 37 | public Shipping getShipping() { 38 | return shipping; 39 | } 40 | public void setShipping(Shipping shipping) { 41 | this.shipping = shipping; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetooneunidirectional/entity/Shipping.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetooneunidirectional.entity; 2 | import javax.persistence.Column; 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.GenerationType; 6 | import javax.persistence.Id; 7 | import javax.persistence.Table; 8 | @Entity 9 | @Table(name="SHIPPING") 10 | public class Shipping { 11 | private Integer id; 12 | private String city; 13 | @Id 14 | @GeneratedValue(strategy=GenerationType.AUTO) 15 | @Column(name="id",length=20) 16 | public Integer getId() { 17 | return id; 18 | } 19 | public void setId(Integer id) { 20 | this.id = id; 21 | } 22 | @Column(name="city",length=50) 23 | public String getCity() { 24 | return city; 25 | } 26 | public void setCity(String city) { 27 | this.city = city; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetooneunidirectional/mapper/BookMapper.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetooneunidirectional.mapper; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import com.oodd.spring.onetooneunidirectional.entity.Book; 6 | import com.oodd.spring.onetooneunidirectional.entity.Shipping; 7 | import com.oodd.spring.onetooneunidirectional.dto.BookDto; 8 | @Component 9 | public class BookMapper { 10 | public Book mapDtoToEntity(BookDto bookDto){ 11 | Book book = new Book(); 12 | Shipping shipping = new Shipping(); 13 | if(null!=bookDto.getId())book.setId(bookDto.getId()); 14 | if(null!=bookDto.getName())book.setName(bookDto.getName()); 15 | if(null!=bookDto.getCity()){shipping.setCity(bookDto.getCity());book.setShipping(shipping);} 16 | return book; 17 | } 18 | public BookDto mapEntityToDto(Book book){ 19 | BookDto bookDto = new BookDto(); 20 | if(null!=book.getId())bookDto.setId(book.getId()); 21 | if(null!=book.getName())bookDto.setName(book.getName()); 22 | if(null!=book.getShipping()){ 23 | if(null !=book.getShipping().getCity()){ 24 | bookDto.setCity(book.getShipping().getCity()); 25 | } 26 | } 27 | return bookDto; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetooneunidirectional/mock/BookInMemoryDB.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetooneunidirectional.mock; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.oodd.spring.onetooneunidirectional.dto.BookDto; 7 | public enum BookInMemoryDB { 8 | INSTANCE; 9 | private static List list = new ArrayList(); 10 | private static Integer lastId = 0; 11 | public Integer getId() { 12 | return ++lastId; 13 | } 14 | public void add(BookDto bookDto){ 15 | bookDto.setId(getId()); 16 | list.add(bookDto); 17 | } 18 | public void edit(BookDto bookDto){ 19 | for(BookDto dto : list){ 20 | if(dto.getId() == bookDto.getId()){ 21 | dto.setName(bookDto.getName()); 22 | dto.setCity(bookDto.getCity()); 23 | } 24 | } 25 | } 26 | public void remove(Integer id) { 27 | BookDto dto = null; 28 | for(BookDto bookDto : list){ 29 | if(bookDto.getId() == id){ 30 | dto = bookDto; 31 | break; 32 | } 33 | } 34 | if(null != dto){list.remove(dto);} 35 | } 36 | public List findAll(){ 37 | return list; 38 | } 39 | public BookDto findById(Integer id) { 40 | for(BookDto bookDto : list){ 41 | if(bookDto.getId() == id) return bookDto ; 42 | } 43 | return null; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetooneunidirectional/service/BookService.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetooneunidirectional.service; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.onetooneunidirectional.dto.BookDto; 6 | public interface BookService { 7 | public void create(BookDto bookDto ) ; 8 | public List findAll(); 9 | public BookDto findById(int id); 10 | public void remove(int productId); 11 | public void edit(BookDto bookDto); 12 | } 13 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/onetooneunidirectional/service/impl/BookServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.onetooneunidirectional.service.impl; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import com.oodd.spring.onetooneunidirectional.dao.BookDao; 11 | import com.oodd.spring.onetooneunidirectional.dto.BookDto; 12 | import com.oodd.spring.onetooneunidirectional.entity.Book; 13 | import com.oodd.spring.onetooneunidirectional.mapper.BookMapper; 14 | import com.oodd.spring.onetooneunidirectional.service.BookService; 15 | @Service 16 | @Transactional 17 | public class BookServiceImpl implements BookService { 18 | @Autowired 19 | private BookDao dao; 20 | @Autowired 21 | private BookMapper mapper; 22 | @Override 23 | public void create(BookDto bookDto) { 24 | dao.insert(mapper.mapDtoToEntity(bookDto)); 25 | } 26 | 27 | @Override 28 | public List findAll() { 29 | List books = dao.getAll(); 30 | List bookDtos = new ArrayList(); 31 | for(Book book : books){ 32 | bookDtos.add(mapper.mapEntityToDto(book)); 33 | } 34 | return bookDtos; 35 | } 36 | 37 | @Override 38 | public BookDto findById(int id) { 39 | Book book = dao.getById(id); 40 | if(null !=book){ 41 | return mapper.mapEntityToDto(book); 42 | } 43 | return null; 44 | } 45 | 46 | @Override 47 | public void remove(int id) { 48 | Book book = dao.getById(id); 49 | dao.delete(book); 50 | } 51 | 52 | @Override 53 | public void edit(BookDto bookDto) { 54 | dao.update(mapper.mapDtoToEntity(bookDto)); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/singletableinheritance/dao/ProtocolDao.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.singletableinheritance.dao; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.singletableinheritance.entity.Protocol; 6 | 7 | public interface ProtocolDao { 8 | public void insert(Protocol protocol) ; 9 | public List getAll(); 10 | public Protocol getById(Integer id); 11 | public void delete(Protocol protocol) ; 12 | public void update(Protocol protocol); 13 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/singletableinheritance/dao/impl/ProtocolDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.singletableinheritance.dao.impl; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Repository; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import com.oodd.spring.singletableinheritance.dao.ProtocolDao; 11 | import com.oodd.spring.singletableinheritance.entity.Protocol; 12 | 13 | @Repository 14 | @Transactional 15 | public class ProtocolDaoImpl implements ProtocolDao { 16 | @Autowired 17 | private SessionFactory sessionFactory ; 18 | @Override 19 | public void insert(Protocol protocol) { 20 | sessionFactory.getCurrentSession().save(protocol); 21 | } 22 | @SuppressWarnings("unchecked") 23 | @Override 24 | public List getAll() { 25 | return sessionFactory.getCurrentSession().createQuery("select protocol from Protocol protocol order by protocol.id desc").list(); 26 | } 27 | @Override 28 | public Protocol getById(Integer id) { 29 | return (Protocol) sessionFactory.getCurrentSession().get(Protocol.class, id); 30 | } 31 | @Override 32 | public void delete(Protocol protocol) { 33 | sessionFactory.getCurrentSession().delete(protocol); 34 | } 35 | @Override 36 | public void update(Protocol protocol) { 37 | sessionFactory.getCurrentSession().merge(protocol); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/singletableinheritance/dto/ProtocolDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.singletableinheritance.dto; 2 | 3 | import com.fasterxml.jackson.annotation.JsonSubTypes; 4 | import com.fasterxml.jackson.annotation.JsonSubTypes.Type; 5 | import com.fasterxml.jackson.annotation.JsonTypeInfo; 6 | 7 | @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type") 8 | @JsonSubTypes({ 9 | @Type(value = TCPDto.class, name = "tcp"), 10 | @Type(value = SNMPDto.class, name = "snmp") }) 11 | public class ProtocolDto { 12 | private Integer id; 13 | private String name; 14 | 15 | public Integer getId() { 16 | return id; 17 | } 18 | public void setId(Integer id) { 19 | this.id = id; 20 | } 21 | public String getName() { 22 | return name; 23 | } 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/singletableinheritance/dto/SNMPDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.singletableinheritance.dto; 2 | 3 | public class SNMPDto extends ProtocolDto { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/singletableinheritance/dto/TCPDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.singletableinheritance.dto; 2 | 3 | public class TCPDto extends ProtocolDto { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/singletableinheritance/entity/Protocol.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.singletableinheritance.entity; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.DiscriminatorColumn; 5 | import javax.persistence.DiscriminatorType; 6 | import javax.persistence.DiscriminatorValue; 7 | import javax.persistence.Entity; 8 | import javax.persistence.GeneratedValue; 9 | import javax.persistence.GenerationType; 10 | import javax.persistence.Id; 11 | import javax.persistence.Inheritance; 12 | import javax.persistence.InheritanceType; 13 | import javax.persistence.Table; 14 | 15 | @Entity 16 | @Table(name = "PROTOCOL") 17 | @Inheritance(strategy = InheritanceType.SINGLE_TABLE) 18 | @DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.STRING) 19 | @DiscriminatorValue(value = "PROTOCOL") 20 | public class Protocol { 21 | 22 | private Integer id; 23 | private String name; 24 | 25 | @Id 26 | @GeneratedValue(strategy = GenerationType.AUTO) 27 | @Column(name = "ID") 28 | public Integer getId() { 29 | return id; 30 | } 31 | 32 | public void setId(Integer id) { 33 | this.id = id; 34 | } 35 | 36 | @Column(name = "NAME", nullable = false, length = 100) 37 | public String getName() { 38 | return name; 39 | } 40 | 41 | public void setName(String name) { 42 | this.name = name; 43 | } 44 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/singletableinheritance/entity/SNMP.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.singletableinheritance.entity; 2 | 3 | import javax.persistence.DiscriminatorValue; 4 | import javax.persistence.Entity; 5 | import javax.persistence.Table; 6 | 7 | @Entity 8 | @Table(name="PROTOCOL") 9 | @DiscriminatorValue("SNMP") 10 | public class SNMP extends Protocol { 11 | 12 | } 13 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/singletableinheritance/entity/TCP.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.singletableinheritance.entity; 2 | 3 | import javax.persistence.DiscriminatorValue; 4 | import javax.persistence.Entity; 5 | import javax.persistence.Table; 6 | 7 | @Entity 8 | @Table(name="PROTOCOL") 9 | @DiscriminatorValue("TCP") 10 | public class TCP extends Protocol { 11 | 12 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/singletableinheritance/mapper/ProtocolMapper.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.singletableinheritance.mapper; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import com.oodd.spring.singletableinheritance.dto.ProtocolDto; 6 | import com.oodd.spring.singletableinheritance.dto.SNMPDto; 7 | import com.oodd.spring.singletableinheritance.dto.TCPDto; 8 | import com.oodd.spring.singletableinheritance.entity.Protocol; 9 | import com.oodd.spring.singletableinheritance.entity.SNMP; 10 | import com.oodd.spring.singletableinheritance.entity.TCP; 11 | 12 | @Component 13 | public class ProtocolMapper { 14 | 15 | public Protocol mapDtoToEntity(ProtocolDto protocolDto){ 16 | if(protocolDto instanceof TCPDto) { 17 | TCP tcp = new TCP(); 18 | if(null!=protocolDto.getId()) tcp.setId(protocolDto.getId()); 19 | if(null!=protocolDto.getName()) tcp.setName(protocolDto.getName()); 20 | return tcp; 21 | } else if(protocolDto instanceof SNMPDto) { 22 | SNMP snmp = new SNMP(); 23 | if(null!=protocolDto.getId()) snmp.setId(protocolDto.getId()); 24 | if(null!=protocolDto.getName()) snmp.setName(protocolDto.getName()); 25 | return snmp; 26 | } 27 | return null; 28 | } 29 | public ProtocolDto mapEntityToDto(Protocol protocol){ 30 | if(protocol instanceof TCP) { 31 | TCPDto tcpDto = new TCPDto(); 32 | if(null!=protocol.getId()) tcpDto.setId(protocol.getId()); 33 | if(null!=protocol.getName()) tcpDto.setName(protocol.getName()); 34 | return tcpDto; 35 | } else if(protocol instanceof SNMP) { 36 | SNMPDto snmpDto = new SNMPDto(); 37 | if(null!=protocol.getId()) snmpDto.setId(protocol.getId()); 38 | if(null!=protocol.getName()) snmpDto.setName(protocol.getName()); 39 | return snmpDto; 40 | } 41 | return null; 42 | } 43 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/singletableinheritance/mock/ProtocolInMemoryDB.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.singletableinheritance.mock; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.oodd.spring.singletableinheritance.dto.ProtocolDto; 7 | 8 | public enum ProtocolInMemoryDB { 9 | 10 | INSTANCE; 11 | 12 | private static List list = new ArrayList(); 13 | private static Integer lastId = 0; 14 | 15 | public Integer getId() { 16 | return ++lastId; 17 | } 18 | 19 | public void add(ProtocolDto protocolDto) { 20 | protocolDto.setId(getId()); 21 | list.add(protocolDto); 22 | } 23 | 24 | public void edit(ProtocolDto protocolDto) { 25 | for (ProtocolDto dto:list) { 26 | if (dto.getId()==protocolDto.getId()) 27 | dto.setName(protocolDto.getName()); 28 | } 29 | } 30 | 31 | public void remove(Integer id) { 32 | ProtocolDto toRemove = null; 33 | for (ProtocolDto dto:list) 34 | if (dto.getId()==id) toRemove = dto; 35 | if (toRemove!=null) list.remove(toRemove); 36 | } 37 | 38 | public List findAll() { 39 | return list; 40 | } 41 | 42 | public ProtocolDto findById(Integer id) { 43 | for (ProtocolDto dto:list) { 44 | if (dto.getId()==id) 45 | return dto; 46 | } 47 | return null; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/singletableinheritance/service/ProtocolService.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.singletableinheritance.service; 2 | 3 | import java.util.List; 4 | 5 | import com.oodd.spring.singletableinheritance.dto.ProtocolDto; 6 | 7 | public interface ProtocolService { 8 | public void create(ProtocolDto protocolDto); 9 | public List findAll(); 10 | public ProtocolDto findById(Integer id); 11 | public void remove(Integer protocolId); 12 | public void edit(ProtocolDto protocolDto); 13 | } -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/standalone/dao/ProductDao.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.standalone.dao; 2 | 3 | import java.util.List; 4 | import com.oodd.spring.standalone.entity.Product; 5 | 6 | public interface ProductDao { 7 | public void insert(Product product ) ; 8 | public List getAll(); 9 | public Product getById(Integer id); 10 | public void delete(Product product) ; 11 | public void update(Product product); 12 | } 13 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/standalone/dao/impl/ProductDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.standalone.dao.impl; 2 | 3 | import java.util.List; 4 | import org.hibernate.SessionFactory; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Repository; 7 | import org.springframework.transaction.annotation.Transactional; 8 | import com.oodd.spring.standalone.dao.ProductDao; 9 | import com.oodd.spring.standalone.entity.Product; 10 | 11 | @Repository 12 | @Transactional 13 | public class ProductDaoImpl implements ProductDao { 14 | @Autowired 15 | private SessionFactory sessionFactory ; 16 | @Override 17 | public void insert(Product product) { 18 | sessionFactory.getCurrentSession().save(product); 19 | } 20 | @SuppressWarnings("unchecked") 21 | @Override 22 | public List getAll() { 23 | return sessionFactory.getCurrentSession().createQuery("select product from Product product order by product.id desc").list(); 24 | } 25 | @Override 26 | public Product getById(Integer id) { 27 | return (Product) sessionFactory.getCurrentSession().get(Product.class, id); 28 | } 29 | @Override 30 | public void delete(Product product) { 31 | sessionFactory.getCurrentSession().delete(product); 32 | } 33 | @Override 34 | public void update(Product product) { 35 | sessionFactory.getCurrentSession().merge(product); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/standalone/dto/ProductDto.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.standalone.dto; 2 | 3 | import java.io.Serializable; 4 | 5 | public class ProductDto implements Serializable { 6 | 7 | private static final long serialVersionUID = 1L; 8 | private Integer id; 9 | private String name; 10 | 11 | public Integer getId() { 12 | return id; 13 | } 14 | public void setId(Integer id) { 15 | this.id = id; 16 | } 17 | public String getName() { 18 | return name; 19 | } 20 | public void setName(String name) { 21 | this.name = name; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/standalone/entity/Product.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.standalone.entity; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Id; 8 | import javax.persistence.Table; 9 | 10 | @Entity 11 | @Table(name="PRODUCT") 12 | public class Product { 13 | 14 | private Integer id; 15 | private String name; 16 | 17 | @Id 18 | @GeneratedValue(strategy=GenerationType.AUTO) 19 | @Column(name="ID") 20 | public Integer getId() { 21 | return id; 22 | } 23 | public void setId(Integer id) { 24 | this.id = id; 25 | } 26 | @Column(name="NAME", nullable=false, length=100) 27 | public String getName() { 28 | return name; 29 | } 30 | public void setName(String name) { 31 | this.name = name; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/standalone/mapper/ProductMapper.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.standalone.mapper; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import com.oodd.spring.standalone.dto.ProductDto; 6 | import com.oodd.spring.standalone.entity.Product; 7 | 8 | @Component 9 | public class ProductMapper { 10 | 11 | public Product mapDtoToEntity(ProductDto productDto){ 12 | Product product = new Product(); 13 | if(null!=productDto.getId()) product.setId(productDto.getId()); 14 | if(null!=productDto.getName()) product.setName(productDto.getName()); 15 | return product; 16 | } 17 | public ProductDto mapEntityToDto(Product product){ 18 | ProductDto productDto = new ProductDto(); 19 | if(null!=product.getId()) productDto.setId(product.getId()); 20 | if(null!=product.getName()) productDto.setName(product.getName()); 21 | return productDto; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/standalone/mock/ProductInMemoryDB.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.standalone.mock; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.oodd.spring.standalone.dto.ProductDto; 7 | 8 | public enum ProductInMemoryDB { 9 | 10 | INSTANCE; 11 | 12 | private static List list = new ArrayList(); 13 | private static Integer lastId = 0; 14 | 15 | public Integer getId() { 16 | return ++lastId; 17 | } 18 | 19 | public void add(ProductDto productDto) { 20 | productDto.setId(getId()); 21 | list.add(productDto); 22 | } 23 | 24 | public void edit(ProductDto productDto) { 25 | for (ProductDto dto:list) { 26 | if (dto.getId()==productDto.getId()) 27 | dto.setName(productDto.getName()); 28 | } 29 | } 30 | 31 | public void remove(Integer id) { 32 | ProductDto toRemove = null; 33 | for (ProductDto dto:list) 34 | if (dto.getId()==id) toRemove = dto; 35 | if (toRemove!=null) list.remove(toRemove); 36 | } 37 | 38 | public List findAll() { 39 | return list; 40 | } 41 | 42 | public ProductDto findById(Integer id) { 43 | for (ProductDto dto:list) { 44 | if (dto.getId()==id) 45 | return dto; 46 | } 47 | return null; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/java/com/oodd/spring/standalone/service/ProductService.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.standalone.service; 2 | 3 | import java.util.List; 4 | import com.oodd.spring.standalone.dto.ProductDto; 5 | 6 | public interface ProductService { 7 | public void create(ProductDto productDto ) ; 8 | public List findAll(); 9 | public ProductDto findById(Integer id); 10 | public void remove(Integer productId); 11 | public void edit(ProductDto productDto); 12 | } 13 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/resources/datasource.properties: -------------------------------------------------------------------------------- 1 | hibernate.connection.driver_class=com.mysql.jdbc.Driver 2 | hibernate.connection.url=jdbc:mysql://localhost:3306/spring 3 | hibernate.connection.username=spring 4 | hibernate.connection.password=spring 5 | 6 | hibernate.c3p0.min_size=5 7 | hibernate.c3p0.max_size=5 8 | hibernate.c3p0.timeout=1800 9 | hibernate.c3p0.max_statements=0 10 | 11 | hibernate.dialect=org.hibernate.dialect.MySQLDialect 12 | 13 | hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider 14 | hibernate.jdbc.batch_size=50 15 | hibernate.show_sql=true 16 | 17 | hibernate.hbm2ddl.auto=create -------------------------------------------------------------------------------- /Spring-OODD/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | spring 9 | 10 | org.springframework.web.servlet.DispatcherServlet 11 | 12 | 1 13 | 14 | 15 | spring 16 | / 17 | 18 | 19 | /jsp/main.jsp 20 | 21 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/webapp/jsp/concretetableinheritance/estate.jsp: -------------------------------------------------------------------------------- 1 | <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 2 | <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 3 | <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 |
14 |

Estate Manager:

15 |
16 |
17 | 18 | 19 | 20 | 21 | 28 | 29 | 30 | 31 | 35 | 36 | 37 | 38 | 41 | 42 | 43 | 44 | 45 | 46 |
Choose Estate 22 | 27 |
Name: 32 | 33 | 34 |
Enter Value
39 | 40 |
47 |
48 |
49 |
50 | 51 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/webapp/jsp/concretetableinheritance/estatemock.jsp: -------------------------------------------------------------------------------- 1 | <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 2 | <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 3 | <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 |
14 |

Estate Manager:

15 |
16 |
17 | 18 | 19 | 20 | 21 | 28 | 29 | 30 | 31 | 35 | 36 | 37 | 38 | 41 | 42 | 43 | 44 | 45 | 46 |
Choose Estate 22 | 27 |
Name: 32 | 33 | 34 |
Enter Value
39 | 40 |
47 |
48 |
49 |
50 | 51 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/webapp/jsp/header.jsp: -------------------------------------------------------------------------------- 1 | 2 |

Spring, Hibernate, Data Modeling, REST and TDD: Agile Java Design and Development

3 | 4 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/webapp/jsp/main.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/webapp/jsp/onetomanybidirectional/item.jsp: -------------------------------------------------------------------------------- 1 | <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 2 | <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 3 | <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | 7 | 8 | 10 | 11 | 12 |
13 |
14 |

15 | Item Manager: 16 |

17 |
18 |
19 |
20 |
21 | 22 | 23 |
24 |
25 |
26 |
27 | 28 | 29 |
30 |
31 |
32 |
33 |
34 |
35 | 36 | 37 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/webapp/jsp/onetomanybidirectional/itemmock.jsp: -------------------------------------------------------------------------------- 1 | <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 2 | <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 3 | <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | 7 | 8 | 10 | 11 | 12 |
13 |
14 |

15 | Item Manager: 16 |

17 |
18 |
19 |
20 |
21 | 22 | 23 |
24 |
25 |
26 |
27 | 28 | 29 |
30 |
31 |
32 |
33 |
34 |
35 | 36 | 37 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/webapp/jsp/onetomanyselfreference/category.jsp: -------------------------------------------------------------------------------- 1 | <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 2 | <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 3 | <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | 7 | 8 | 10 | 11 | 12 |
13 |
14 |

15 | Category Manager: 16 |

17 |
18 |
19 |
20 |
21 | 22 | 23 |
24 |
25 |
26 |
27 |
28 | 29 | 32 |
33 |
34 |
35 |
36 | 38 |
39 |
40 |
41 |
42 | 43 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/webapp/jsp/onetomanyselfreference/categorymock.jsp: -------------------------------------------------------------------------------- 1 | <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 2 | <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 3 | <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | 7 | 8 | 10 | 11 | 12 |
13 |
14 |

15 | Category Manager: 16 |

17 |
18 |
19 |
20 |
21 | 22 | 23 |
24 |
25 |
26 |
27 |
28 | 29 | 32 |
33 |
34 |
35 |
36 | 38 |
39 |
40 |
41 |
42 | 43 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/webapp/jsp/onetomanyunidirectional/person.jsp: -------------------------------------------------------------------------------- 1 | <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 2 | <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 3 | <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | 7 | 8 | 10 | 11 | 12 |
13 |
14 |

15 | Person Manager: 16 |

17 |
18 |
19 |
20 |
21 | 22 | 23 |
24 |
25 |
26 |
27 | 28 | 29 |
30 |
31 |
32 |
33 |
34 |
35 | 36 | 37 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/webapp/jsp/onetomanyunidirectional/personmock.jsp: -------------------------------------------------------------------------------- 1 | <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 2 | <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 3 | <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | 7 | 8 | 10 | 11 | 12 |
13 |
14 |

15 | Person Manager: 16 |

17 |
18 |
19 |
20 |
21 | 22 | 23 |
24 |
25 |
26 |
27 | 28 | 29 |
30 |
31 |
32 |
33 |
34 |
35 | 36 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/webapp/jsp/onetoonebidirectional/customer.jsp: -------------------------------------------------------------------------------- 1 | <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 2 | <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 3 | <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 |
15 |

16 | Customer Manager: 17 |

18 |
19 |
20 | 21 | 22 | 23 | 24 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | 35 | 36 |
Name:
37 |
38 |
39 |
40 | 41 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/webapp/jsp/onetoonebidirectional/customermock.jsp: -------------------------------------------------------------------------------- 1 | <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 2 | <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 3 | <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 |
14 |

15 | Customer Manager: 16 |

17 |
18 |
19 | 20 | 21 | 22 | 23 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 33 | 34 | 35 |
Name:
36 |
37 |
38 |
39 | 40 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/webapp/jsp/onetooneselfreference/student.jsp: -------------------------------------------------------------------------------- 1 | <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 2 | <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 3 | <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 |
15 |

16 | Student Manager: 17 |

18 |
19 |
20 | 21 | 22 | 23 | 24 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | 35 | 36 |
Name:
37 |
38 |
39 |
40 | 41 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/webapp/jsp/onetooneselfreference/studentmock.jsp: -------------------------------------------------------------------------------- 1 | <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 2 | <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 3 | <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 |
15 |

16 | Student Manager: 17 |

18 |
19 |
20 | 21 | 22 | 23 | 24 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | 35 | 36 |
Name:
37 |
38 |
39 |
40 | 41 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/webapp/jsp/onetooneunidirectional/book.jsp: -------------------------------------------------------------------------------- 1 | <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 2 | <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 3 | <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | 7 | 8 | 10 | 11 | 12 | 13 | 14 |
15 |
16 |

17 | Book Manager: 18 |

19 |
20 |
21 | 22 | 23 | 24 | 25 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 35 | 36 | 37 |
Name:
38 |
39 |
40 |
41 | 42 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/webapp/jsp/onetooneunidirectional/bookmock.jsp: -------------------------------------------------------------------------------- 1 | <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 2 | <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 3 | <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | 7 | 8 | 10 | 11 | 12 | 13 | 14 |
15 |
16 |

17 | Book Manager: 18 |

19 |
20 |
21 | 22 | 23 | 24 | 25 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 35 | 36 | 37 |
Name:
38 |
39 |
40 |
41 | 42 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/webapp/jsp/singletableinheritance/protocol.jsp: -------------------------------------------------------------------------------- 1 | <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 2 | <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 3 | <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 |
14 |

Protocol Manager:

15 |
16 |
17 | 18 | 19 | 20 | 21 | 28 | 29 | 30 | 31 | 35 | 36 | 37 | 38 |
Choose Protocol 22 | 27 |
Name: 32 | 33 | 34 |
39 |
40 |
41 |
42 | 43 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/webapp/jsp/singletableinheritance/protocolmock.jsp: -------------------------------------------------------------------------------- 1 | <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 2 | <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 3 | <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 |
14 |

Protocol Manager:

15 |
16 |
17 | 18 | 19 | 20 | 21 | 28 | 29 | 30 | 31 | 35 | 36 | 37 | 38 |
Choose Protocol 22 | 27 |
Name: 32 | 33 | 34 |
39 |
40 |
41 |
42 | 43 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/webapp/jsp/standalone/product.jsp: -------------------------------------------------------------------------------- 1 | <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 2 | <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 3 | <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 |
14 |

Product Manager:

15 |
16 |
17 | 18 | 19 | 20 | 21 | 25 | 26 | 27 | 28 |
Name: 22 | 23 | 24 |
29 |
30 |
31 |
32 | 33 | -------------------------------------------------------------------------------- /Spring-OODD/src/main/webapp/jsp/standalone/productmock.jsp: -------------------------------------------------------------------------------- 1 | <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 2 | <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 3 | <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 |
14 |

Product Manager:

15 |
16 |
17 | 18 | 19 | 20 | 21 | 25 | 26 | 27 | 28 |
Name: 22 | 23 | 24 |
29 |
30 |
31 |
32 | 33 | -------------------------------------------------------------------------------- /Spring-OODD/src/test/java/com/oodd/spring/classtableinheritance/entity/EmployeeTest.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.classtableinheritance.entity; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.test.context.ContextConfiguration; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | import org.springframework.test.context.transaction.TransactionConfiguration; 13 | import org.springframework.transaction.annotation.Transactional; 14 | 15 | @RunWith( SpringJUnit4ClassRunner.class ) 16 | @ContextConfiguration( locations = { "classpath:context.xml" } ) 17 | @TransactionConfiguration( defaultRollback = true ) 18 | @Transactional 19 | public class EmployeeTest { 20 | @Autowired 21 | private SessionFactory sessionFactory; 22 | 23 | @SuppressWarnings("unchecked") 24 | @Test 25 | public void testCRUD() 26 | { 27 | Employee e1 = new Employee(); 28 | e1.setName("A"); 29 | 30 | Employee e2 = new Employee(); 31 | e2.setName("B"); 32 | 33 | sessionFactory.getCurrentSession().save(e1); 34 | sessionFactory.getCurrentSession().save(e2); 35 | 36 | e1.setName("C"); 37 | sessionFactory.getCurrentSession().merge(e1); 38 | 39 | List list = sessionFactory.getCurrentSession().createQuery("from Employee").list(); 40 | Assert.assertEquals(2L, list.size()); 41 | 42 | sessionFactory.getCurrentSession().delete(e1); 43 | 44 | List list2 = sessionFactory.getCurrentSession().createQuery("from Employee").list(); 45 | Assert.assertEquals(1L, list2.size()); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Spring-OODD/src/test/java/com/oodd/spring/concretetableinheritance/entity/EstateTest.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.concretetableinheritance.entity; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.test.context.ContextConfiguration; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | import org.springframework.test.context.transaction.TransactionConfiguration; 13 | import org.springframework.transaction.annotation.Transactional; 14 | 15 | import com.oodd.spring.concretetableinheritance.entity.Estate; 16 | 17 | @RunWith( SpringJUnit4ClassRunner.class ) 18 | @ContextConfiguration( locations = { "classpath:context.xml" } ) 19 | @TransactionConfiguration( defaultRollback = true ) 20 | @Transactional 21 | public class EstateTest { 22 | @Autowired 23 | private SessionFactory sessionFactory; 24 | 25 | @SuppressWarnings("unchecked") 26 | @Test 27 | public void testCRUD() 28 | { 29 | Estate p1 = new Estate(); 30 | p1.setName("A"); 31 | 32 | Estate p2 = new Estate(); 33 | p2.setName("B"); 34 | 35 | sessionFactory.getCurrentSession().save(p1); 36 | sessionFactory.getCurrentSession().save(p2); 37 | 38 | p1.setName("C"); 39 | sessionFactory.getCurrentSession().merge(p1); 40 | 41 | List list = sessionFactory.getCurrentSession().createQuery("from Estate").list(); 42 | Assert.assertEquals(2L, list.size()); 43 | 44 | sessionFactory.getCurrentSession().delete(p1); 45 | 46 | List list2 = sessionFactory.getCurrentSession().createQuery("from Estate").list(); 47 | Assert.assertEquals(1L, list2.size()); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Spring-OODD/src/test/java/com/oodd/spring/manytomanybidirectionalwithjoinattribute/entity/AuthorTest.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanybidirectionalwithjoinattribute.entity; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.test.context.ContextConfiguration; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | import org.springframework.test.context.transaction.TransactionConfiguration; 13 | import org.springframework.transaction.annotation.Transactional; 14 | 15 | @RunWith( SpringJUnit4ClassRunner.class ) 16 | @ContextConfiguration( locations = { "classpath:context.xml" } ) 17 | @TransactionConfiguration( defaultRollback = true ) 18 | @Transactional 19 | public class AuthorTest { 20 | 21 | @Autowired 22 | private SessionFactory sessionFactory; 23 | 24 | @SuppressWarnings("unchecked") 25 | @Test 26 | public void testCRUD() { 27 | Author author1 = new Author(); 28 | author1.setName("Amritendu De"); 29 | 30 | Author author2 = new Author(); 31 | author2.setName("J. R. R. Tolkien"); 32 | 33 | sessionFactory.getCurrentSession().save(author1); 34 | sessionFactory.getCurrentSession().save(author2); 35 | 36 | author1.setName("Amish Tripathi"); 37 | sessionFactory.getCurrentSession().merge(author1); 38 | 39 | List list = sessionFactory.getCurrentSession().createQuery("from Author").list(); 40 | Assert.assertEquals(2L, list.size()); 41 | 42 | sessionFactory.getCurrentSession().delete(author1); 43 | 44 | List list2 = sessionFactory.getCurrentSession().createQuery("from Author").list(); 45 | Assert.assertEquals(1L, list2.size()); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Spring-OODD/src/test/java/com/oodd/spring/manytomanyselfreferencewithjoinattribute/entity/WorkerTest.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyselfreferencewithjoinattribute.entity; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.test.context.ContextConfiguration; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | import org.springframework.test.context.transaction.TransactionConfiguration; 13 | import org.springframework.transaction.annotation.Transactional; 14 | 15 | @RunWith( SpringJUnit4ClassRunner.class ) 16 | @ContextConfiguration( locations = { "classpath:context.xml" } ) 17 | @TransactionConfiguration( defaultRollback = true ) 18 | @Transactional 19 | public class WorkerTest { 20 | 21 | @Autowired 22 | private SessionFactory sessionFactory; 23 | 24 | @SuppressWarnings("unchecked") 25 | @Test 26 | public void testCRUD() { 27 | Worker worker1 = new Worker(); 28 | worker1.setName("Lalit Narayan Mishra"); 29 | 30 | Worker worker2 = new Worker(); 31 | worker2.setName("Amritendu De"); 32 | 33 | sessionFactory.getCurrentSession().save(worker1); 34 | sessionFactory.getCurrentSession().save(worker2); 35 | 36 | worker1.setName("Amish Tripathi"); 37 | sessionFactory.getCurrentSession().merge(worker1); 38 | 39 | List list = sessionFactory.getCurrentSession().createQuery("from Worker").list(); 40 | Assert.assertEquals(2L, list.size()); 41 | 42 | sessionFactory.getCurrentSession().delete(worker1); 43 | 44 | List list2 = sessionFactory.getCurrentSession().createQuery("from Worker").list(); 45 | Assert.assertEquals(1L, list2.size()); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Spring-OODD/src/test/java/com/oodd/spring/manytomanyunidirectional/entity/GroupTest.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.manytomanyunidirectional.entity; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.test.context.ContextConfiguration; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | import org.springframework.test.context.transaction.TransactionConfiguration; 13 | import org.springframework.transaction.annotation.Transactional; 14 | 15 | @RunWith( SpringJUnit4ClassRunner.class ) 16 | @ContextConfiguration( locations = { "classpath:context.xml" } ) 17 | @TransactionConfiguration( defaultRollback = true ) 18 | @Transactional 19 | public class GroupTest { 20 | @Autowired 21 | private SessionFactory sessionFactory; 22 | 23 | @SuppressWarnings("unchecked") 24 | @Test 25 | public void testCRUD() 26 | { 27 | Group g1 = new Group(); 28 | g1.setName("A"); 29 | 30 | Group g2 = new Group(); 31 | g2.setName("B"); 32 | 33 | sessionFactory.getCurrentSession().save(g1); 34 | sessionFactory.getCurrentSession().save(g2); 35 | 36 | g1.setName("C"); 37 | sessionFactory.getCurrentSession().merge(g2); 38 | 39 | List list = sessionFactory.getCurrentSession().createQuery("from Group").list(); 40 | Assert.assertEquals(2L, list.size()); 41 | 42 | sessionFactory.getCurrentSession().delete(g1); 43 | 44 | List list2 = sessionFactory.getCurrentSession().createQuery("from Group").list(); 45 | Assert.assertEquals(1L, list2.size()); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Spring-OODD/src/test/java/com/oodd/spring/singletableinheritance/entity/ProtocolTest.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.singletableinheritance.entity; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.test.context.ContextConfiguration; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | import org.springframework.test.context.transaction.TransactionConfiguration; 13 | import org.springframework.transaction.annotation.Transactional; 14 | 15 | @RunWith( SpringJUnit4ClassRunner.class ) 16 | @ContextConfiguration( locations = { "classpath:context.xml" } ) 17 | @TransactionConfiguration( defaultRollback = true ) 18 | @Transactional 19 | public class ProtocolTest { 20 | @Autowired 21 | private SessionFactory sessionFactory; 22 | 23 | @SuppressWarnings("unchecked") 24 | @Test 25 | public void testCRUD() 26 | { 27 | Protocol p1 = new Protocol(); 28 | p1.setName("A"); 29 | 30 | Protocol p2 = new Protocol(); 31 | p2.setName("B"); 32 | 33 | sessionFactory.getCurrentSession().save(p1); 34 | sessionFactory.getCurrentSession().save(p2); 35 | 36 | p1.setName("C"); 37 | sessionFactory.getCurrentSession().merge(p1); 38 | 39 | List list = sessionFactory.getCurrentSession().createQuery("from Protocol").list(); 40 | Assert.assertEquals(2L, list.size()); 41 | 42 | sessionFactory.getCurrentSession().delete(p1); 43 | 44 | List list2 = sessionFactory.getCurrentSession().createQuery("from Protocol").list(); 45 | Assert.assertEquals(1L, list2.size()); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Spring-OODD/src/test/java/com/oodd/spring/singletableinheritance/entity/SNMPTest.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.singletableinheritance.entity; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.test.context.ContextConfiguration; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | import org.springframework.test.context.transaction.TransactionConfiguration; 13 | import org.springframework.transaction.annotation.Transactional; 14 | 15 | @RunWith( SpringJUnit4ClassRunner.class ) 16 | @ContextConfiguration( locations = { "classpath:context.xml" } ) 17 | @TransactionConfiguration( defaultRollback = true ) 18 | @Transactional 19 | public class SNMPTest { 20 | @Autowired 21 | private SessionFactory sessionFactory; 22 | 23 | @SuppressWarnings("unchecked") 24 | @Test 25 | public void testCRUD() 26 | { 27 | Protocol p1 = new Protocol(); 28 | p1.setName("Protocol"); 29 | sessionFactory.getCurrentSession().save(p1); 30 | 31 | SNMP snmp = new SNMP(); 32 | snmp.setName("SNMP"); 33 | sessionFactory.getCurrentSession().save(snmp); 34 | 35 | snmp.setName("SNMP Protocol"); 36 | sessionFactory.getCurrentSession().merge(snmp); 37 | 38 | List list = sessionFactory.getCurrentSession().createQuery("from SNMP").list(); 39 | Assert.assertEquals(1L, list.size()); 40 | 41 | sessionFactory.getCurrentSession().delete(snmp); 42 | 43 | List list2 = sessionFactory.getCurrentSession().createQuery("from SNMP").list(); 44 | Assert.assertEquals(0L, list2.size()); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Spring-OODD/src/test/java/com/oodd/spring/singletableinheritance/entity/TCPTest.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.singletableinheritance.entity; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.test.context.ContextConfiguration; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | import org.springframework.test.context.transaction.TransactionConfiguration; 13 | import org.springframework.transaction.annotation.Transactional; 14 | 15 | @RunWith( SpringJUnit4ClassRunner.class ) 16 | @ContextConfiguration( locations = { "classpath:context.xml" } ) 17 | @TransactionConfiguration( defaultRollback = true ) 18 | @Transactional 19 | public class TCPTest { 20 | 21 | @Autowired 22 | private SessionFactory sessionFactory; 23 | 24 | @SuppressWarnings("unchecked") 25 | @Test 26 | public void testCRUD() 27 | { 28 | Protocol p1 = new Protocol(); 29 | p1.setName("Protocol"); 30 | sessionFactory.getCurrentSession().save(p1); 31 | 32 | TCP tcp = new TCP(); 33 | tcp.setName("TCP"); 34 | sessionFactory.getCurrentSession().save(tcp); 35 | 36 | tcp.setName("TCP/IP"); 37 | sessionFactory.getCurrentSession().merge(tcp); 38 | 39 | List list = sessionFactory.getCurrentSession().createQuery("from TCP").list(); 40 | Assert.assertEquals(1L, list.size()); 41 | 42 | sessionFactory.getCurrentSession().delete(tcp); 43 | 44 | List list2 = sessionFactory.getCurrentSession().createQuery("from TCP").list(); 45 | Assert.assertEquals(0L, list2.size()); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Spring-OODD/src/test/java/com/oodd/spring/standalone/entity/ProductTest.java: -------------------------------------------------------------------------------- 1 | package com.oodd.spring.standalone.entity; 2 | 3 | import java.util.List; 4 | 5 | import org.hibernate.SessionFactory; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.test.context.ContextConfiguration; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | import org.springframework.test.context.transaction.TransactionConfiguration; 13 | import org.springframework.transaction.annotation.Transactional; 14 | 15 | @RunWith( SpringJUnit4ClassRunner.class ) 16 | @ContextConfiguration( locations = { "classpath:context.xml" } ) 17 | @TransactionConfiguration( defaultRollback = true ) 18 | @Transactional 19 | public class ProductTest { 20 | @Autowired 21 | private SessionFactory sessionFactory; 22 | 23 | @Test 24 | public void testCRUD() 25 | { 26 | Product p1 = new Product(); 27 | p1.setName("A"); 28 | 29 | Product p2 = new Product(); 30 | p2.setName("B"); 31 | 32 | sessionFactory.getCurrentSession().save(p1); 33 | sessionFactory.getCurrentSession().save(p2); 34 | 35 | p1.setName("C"); 36 | sessionFactory.getCurrentSession().merge(p1); 37 | 38 | List list = sessionFactory.getCurrentSession().createQuery("from Product").list(); 39 | Assert.assertEquals(2L, list.size()); 40 | 41 | sessionFactory.getCurrentSession().delete(p1); 42 | 43 | List list2 = sessionFactory.getCurrentSession().createQuery("from Product").list(); 44 | Assert.assertEquals(1L, list2.size()); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Spring-OODD/src/test/resources/datasource.properties: -------------------------------------------------------------------------------- 1 | hibernate.connection.driver_class=com.mysql.jdbc.Driver 2 | hibernate.connection.url=jdbc:mysql://localhost:3306/spring 3 | hibernate.connection.username=spring 4 | hibernate.connection.password=spring 5 | 6 | hibernate.c3p0.min_size=5 7 | hibernate.c3p0.max_size=5 8 | hibernate.c3p0.timeout=1800 9 | hibernate.c3p0.max_statements=0 10 | 11 | hibernate.dialect=org.hibernate.dialect.MySQLDialect 12 | 13 | hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider 14 | hibernate.jdbc.batch_size=50 15 | hibernate.show_sql=true 16 | 17 | hibernate.hbm2ddl.auto=create --------------------------------------------------------------------------------