Advertisement
paulito81

Entity delete with java

Nov 16th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.28 KB | None | 0 0
  1. public class JpaAccountDao implements AccountDao {
  2.  
  3. @PersistenceContext(unitName = "account")
  4. private EntityManager entityManager;
  5.  
  6. public JpaAccountDao() {
  7. }
  8. public JpaAccountDao(EntityManager entityManager){
  9. this.entityManager = entityManager;
  10. }
  11.  
  12. @Override
  13. public Account persist(Account account) {
  14. if( account == null )
  15. throw new IllegalArgumentException("No account could be created!");
  16. entityManager.persist(account);
  17. return account;
  18. }
  19.  
  20. @Override
  21. public Boolean delete(int id) {
  22. if( id != 0) {
  23. Account account = entityManager.find(Account.class,id);
  24. entityManager.remove(account);
  25. return true;
  26. }
  27. throw new IllegalArgumentException(String.format("Account with id-nr:{%d] could not be deleted =C ", id ));
  28. }
  29. @Override
  30. public Account findById(int id) {
  31. if( id <= 0 )
  32. throw new IllegalArgumentException("No id was found!");
  33. return entityManager.find(Account.class, id);
  34. }
  35.  
  36. @Override
  37. public List<Account> getAll() {
  38. TypedQuery<Account> query = entityManager.createNamedQuery("Account.getAll", Account.class);
  39. return query.getResultList();
  40. }
  41. }
  42.  
  43. @Entity
  44. @SequenceGenerator(name = "SEQ_LOG", initialValue = 50)
  45. public class Login {
  46.  
  47. @Id
  48. @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_LOG")
  49. @Column(name = "FK_LOGIN")
  50. private int Id;
  51. @NotNull
  52. private String username;
  53. @NotNull
  54. private String password;
  55.  
  56. public Login(String username, String password) {
  57. this.username = username;
  58. this.password = password;
  59. }
  60. public Login(){
  61.  
  62. }
  63. public int getId() {
  64. return Id;
  65. }
  66. public void setId(int id) {
  67. Id = id;
  68. }
  69. public String getUsername() {
  70. return username;
  71. }
  72.  
  73. public void setUsername(String username) {
  74. this.username = username;
  75. }
  76.  
  77. public String getPassword() {
  78. return password;
  79. }
  80.  
  81. public void setPassword(String password) {
  82. this.password = password;
  83. }
  84. @Override
  85. public String toString() {
  86. return "Login{" +
  87. "Id=" + Id +
  88. ", username='" + username + '\'' +
  89. ", password='" + password + '\'' +
  90. '}';
  91. }
  92. }
  93.  
  94. @Entity
  95. @NamedQuery(name = "Customer.getAll", query = "SELECT c FROM Customer c")
  96. @SequenceGenerator(name = "SEQ_CUST", initialValue = 50)
  97. public class Customer {
  98.  
  99. @Id
  100. @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_CUST")
  101. @Cascade(CascadeType.DETACH)
  102. @Column(name = "FK_CUSTOMER")
  103. private int id;
  104. @NotNull
  105. private String firstName;
  106. @NotNull
  107. private String lastName;
  108. @NotNull
  109. private String email;
  110. @NotNull
  111. private String phoneNumber;
  112. @NotNull
  113. private Date birth;
  114.  
  115. @OneToOne(fetch = FetchType.EAGER, orphanRemoval = true)
  116. @JoinColumn(name = "FK_Account")
  117. private Account account;
  118.  
  119. public Customer(){
  120. }
  121.  
  122. public Customer(String firstName, String lastName, String email, String phoneNumber, Date birth) {
  123. this.firstName = firstName;
  124. this.lastName = lastName;
  125. this.email = email;
  126. this.phoneNumber = phoneNumber;
  127. this.birth = birth;
  128. }
  129.  
  130. public int getId() {
  131. return id;
  132. }
  133.  
  134. public void setId(int id) {
  135. this.id = id;
  136. }
  137.  
  138. public String getFirstName() {
  139. return firstName;
  140. }
  141.  
  142. public void setFirstName(String firstName) {
  143. this.firstName = firstName;
  144. }
  145.  
  146. public String getLastName() {
  147. return lastName;
  148. }
  149.  
  150. public void setLastName(String lastName) {
  151. this.lastName = lastName;
  152. }
  153.  
  154. public String getEmail() {
  155. return email;
  156. }
  157.  
  158. public void setEmail(String email) {
  159. this.email = email;
  160. }
  161.  
  162. public String getPhoneNumber() {
  163. return phoneNumber;
  164. }
  165.  
  166. public void setPhoneNumber(String phoneNumber) {
  167. this.phoneNumber = phoneNumber;
  168. }
  169.  
  170. public Date getBirth() {
  171. return birth;
  172. }
  173.  
  174. public void setBirth(Date birth) {
  175. this.birth = birth;
  176. }
  177. @Override
  178. public String toString() {
  179. return "Customer{" +
  180. "customerId=" + id +
  181. ", firstName='" + firstName + '\'' +
  182. ", lastName='" + lastName + '\'' +
  183. ", email='" + email + '\'' +
  184. ", phoneNumber='" + phoneNumber + '\'' +
  185. ", birth=" + birth +
  186. '}';
  187. }
  188. }
  189.  
  190.  
  191. @Entity
  192. @NamedQuery(name = "Account.getAll", query = "select a from Account a")
  193. @SequenceGenerator(name = "SEQ_ACC", initialValue = 50)
  194. public class Account {
  195.  
  196. @Id
  197. @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_ACC")
  198. private int id;
  199.  
  200. @OneToOne(cascade = CascadeType.ALL)//, fetch = FetchType.EAGER)
  201. @JoinColumn(name = "FK_CUSTOMER")
  202. private Customer customer;
  203.  
  204. @OneToOne(cascade = CascadeType.ALL)
  205. @JoinColumn(name = "FK_LOGIN")
  206. private Login login;
  207.  
  208. public Account(Customer customer, Login login) {
  209. this.customer = customer;
  210. this.login = login;
  211. }
  212.  
  213. public Account() {
  214.  
  215. }
  216. public Customer getCustomer() {
  217. return customer;
  218. }
  219.  
  220. public int getId() {
  221. return id;
  222. }
  223.  
  224. public void setId(int id) {
  225. this.id = id;
  226. }
  227.  
  228. public void setCustomer(Customer customer) {
  229. this.customer = customer;
  230. }
  231.  
  232. public Login getLogin() {
  233. return login;
  234. }
  235.  
  236. public void setLogin(Login login) {
  237. this.login = login;
  238. }
  239. @Override
  240. public String toString() {
  241. return "Account{" +
  242. "id=" + id +
  243. ", customer=" + customer +
  244. ", login= '" + login +
  245. '}';
  246. }
  247. }
  248.  
  249. public class AccountServiceIT {
  250. private EntityManager entityManager;
  251. private EntityManagerFactory factory;
  252. private JpaAccountDao jpaAccountDao;
  253. private JpaCustomerDao jpaCustomerDao;
  254. private CustomerTestCase customerTestCase;
  255. private JpaLoginDao jpaLoginDao;
  256. private Account account;
  257. private Account account2;
  258.  
  259. @Before
  260. public void setup() throws Exception {
  261. factory = Persistence.createEntityManagerFactory("TEST");
  262. entityManager = factory.createEntityManager();
  263. jpaAccountDao = new JpaAccountDao(entityManager);
  264. // customerTestCase = new CustomerTestCase();
  265. // jpaCustomerDao = customerTestCase.setup();
  266.  
  267.  
  268. account = new Account();
  269. account2 = new Account();
  270. }
  271. @After
  272. public void tearDown() throws Exception {
  273. entityManager.close();
  274. factory.close();
  275. }
  276. @Test
  277. public void deleteAccountTest() throws Exception {
  278. Account account = entityManager.find(Account.class, 1);
  279. entityManager.getTransaction().begin();
  280. boolean result = jpaAccountDao.delete(account.getId());
  281. entityManager.getTransaction().commit();
  282.  
  283. Account res = jpaAccountDao.findById(1);
  284. assertEquals(res, account);
  285. assertNull(result);
  286. }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement