Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class JpaAccountDao implements AccountDao {
- @PersistenceContext(unitName = "account")
- private EntityManager entityManager;
- public JpaAccountDao() {
- }
- public JpaAccountDao(EntityManager entityManager){
- this.entityManager = entityManager;
- }
- @Override
- public Account persist(Account account) {
- if( account == null )
- throw new IllegalArgumentException("No account could be created!");
- entityManager.persist(account);
- return account;
- }
- @Override
- public Boolean delete(int id) {
- if( id != 0) {
- Account account = entityManager.find(Account.class,id);
- entityManager.remove(account);
- return true;
- }
- throw new IllegalArgumentException(String.format("Account with id-nr:{%d] could not be deleted =C ", id ));
- }
- @Override
- public Account findById(int id) {
- if( id <= 0 )
- throw new IllegalArgumentException("No id was found!");
- return entityManager.find(Account.class, id);
- }
- @Override
- public List<Account> getAll() {
- TypedQuery<Account> query = entityManager.createNamedQuery("Account.getAll", Account.class);
- return query.getResultList();
- }
- }
- @Entity
- @SequenceGenerator(name = "SEQ_LOG", initialValue = 50)
- public class Login {
- @Id
- @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_LOG")
- @Column(name = "FK_LOGIN")
- private int Id;
- @NotNull
- private String username;
- @NotNull
- private String password;
- public Login(String username, String password) {
- this.username = username;
- this.password = password;
- }
- public Login(){
- }
- public int getId() {
- return Id;
- }
- public void setId(int id) {
- Id = id;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- @Override
- public String toString() {
- return "Login{" +
- "Id=" + Id +
- ", username='" + username + '\'' +
- ", password='" + password + '\'' +
- '}';
- }
- }
- @Entity
- @NamedQuery(name = "Customer.getAll", query = "SELECT c FROM Customer c")
- @SequenceGenerator(name = "SEQ_CUST", initialValue = 50)
- public class Customer {
- @Id
- @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_CUST")
- @Cascade(CascadeType.DETACH)
- @Column(name = "FK_CUSTOMER")
- private int id;
- @NotNull
- private String firstName;
- @NotNull
- private String lastName;
- @NotNull
- private String email;
- @NotNull
- private String phoneNumber;
- @NotNull
- private Date birth;
- @OneToOne(fetch = FetchType.EAGER, orphanRemoval = true)
- @JoinColumn(name = "FK_Account")
- private Account account;
- public Customer(){
- }
- public Customer(String firstName, String lastName, String email, String phoneNumber, Date birth) {
- this.firstName = firstName;
- this.lastName = lastName;
- this.email = email;
- this.phoneNumber = phoneNumber;
- this.birth = birth;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getFirstName() {
- return firstName;
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public String getLastName() {
- return lastName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public String getPhoneNumber() {
- return phoneNumber;
- }
- public void setPhoneNumber(String phoneNumber) {
- this.phoneNumber = phoneNumber;
- }
- public Date getBirth() {
- return birth;
- }
- public void setBirth(Date birth) {
- this.birth = birth;
- }
- @Override
- public String toString() {
- return "Customer{" +
- "customerId=" + id +
- ", firstName='" + firstName + '\'' +
- ", lastName='" + lastName + '\'' +
- ", email='" + email + '\'' +
- ", phoneNumber='" + phoneNumber + '\'' +
- ", birth=" + birth +
- '}';
- }
- }
- @Entity
- @NamedQuery(name = "Account.getAll", query = "select a from Account a")
- @SequenceGenerator(name = "SEQ_ACC", initialValue = 50)
- public class Account {
- @Id
- @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_ACC")
- private int id;
- @OneToOne(cascade = CascadeType.ALL)//, fetch = FetchType.EAGER)
- @JoinColumn(name = "FK_CUSTOMER")
- private Customer customer;
- @OneToOne(cascade = CascadeType.ALL)
- @JoinColumn(name = "FK_LOGIN")
- private Login login;
- public Account(Customer customer, Login login) {
- this.customer = customer;
- this.login = login;
- }
- public Account() {
- }
- public Customer getCustomer() {
- return customer;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public void setCustomer(Customer customer) {
- this.customer = customer;
- }
- public Login getLogin() {
- return login;
- }
- public void setLogin(Login login) {
- this.login = login;
- }
- @Override
- public String toString() {
- return "Account{" +
- "id=" + id +
- ", customer=" + customer +
- ", login= '" + login +
- '}';
- }
- }
- public class AccountServiceIT {
- private EntityManager entityManager;
- private EntityManagerFactory factory;
- private JpaAccountDao jpaAccountDao;
- private JpaCustomerDao jpaCustomerDao;
- private CustomerTestCase customerTestCase;
- private JpaLoginDao jpaLoginDao;
- private Account account;
- private Account account2;
- @Before
- public void setup() throws Exception {
- factory = Persistence.createEntityManagerFactory("TEST");
- entityManager = factory.createEntityManager();
- jpaAccountDao = new JpaAccountDao(entityManager);
- // customerTestCase = new CustomerTestCase();
- // jpaCustomerDao = customerTestCase.setup();
- account = new Account();
- account2 = new Account();
- }
- @After
- public void tearDown() throws Exception {
- entityManager.close();
- factory.close();
- }
- @Test
- public void deleteAccountTest() throws Exception {
- Account account = entityManager.find(Account.class, 1);
- entityManager.getTransaction().begin();
- boolean result = jpaAccountDao.delete(account.getId());
- entityManager.getTransaction().commit();
- Account res = jpaAccountDao.findById(1);
- assertEquals(res, account);
- assertNull(result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement