Testing the Application in Java Paint Data Matrix 2d barcode in Java Testing the Application

Testing the Application using none tocompose none for web,windows application Scan Barcodes with Mobile Phones To test our none for none application we create a session bean interface, BankService, with methods createCustomers() and findCustomer() as follows:. @Remote pub lic interface BankService { void createCustomers(); Customer findCustomer(int custId); }. The createC none none ustomers() method populates and then persists the Customer, Address, Account, and Referee entities. The findCustomer() method, which is invoked after createCustomers(), retrieves a Customer entity for a given customer identifier. Now let"s take a look at some of the code for the interface implementation, the stateless session bean BankServiceBean:.

@Stateless public class BankServiceBean implements BankService { private EntityManager em; ...

public void createCustomers() { [ 71 ]. Object/Rela none for none tional Mapping Referee r1 = new Referee(); r1.setId(1); r1.setName("SIR JOHN DEED"); r1.

setComments("JUDGE"); em.persist(r1); Customer c1 = new Customer(); c1.setId(1); c1.

setFirstName("SIMON"); c1.setLastName("KING"); c1.setReferee(r1); Account a1 = new Account(); a1.

setId(1); a1.setBalance(430.5); a1.

setAccountType("C"); ArrayList<Account> accounts1 = new ArrayList<Account>(); accounts1.add(a1); c1.setAccounts(accounts1); em.

persist(a1); Address add1 = new Address(); add1.setId(1); add1.setAddressLine("49, KINGS ROAD MANCHESTER"); add1.

setCountry("UK"); add1.setPostCode("MN1 2AB"); ArrayList<Address> addresses1 = new ArrayList<Address>(); addresses1.add(add1); c1.

setAddresses(addresses1); em.persist(add1); em.persist(c1); .


In the abov e code we use the Referee and Customer methods to populate their respective entities. We can use the EntityManager.persist() method to persist Referee as soon as its fields are populated.

After populating the Customer entity we do not persist it as we have not yet populated the referenced Account and Address entities. Next, we create a new Account instance a1 and populate its fields. We then create an ArrayList of type Account and add a1 to this list as follows:.

ArrayList&l none none t;Account> accounts1 = new ArrayList<Account>(); accounts1.add(a1);. We can now invoke the Customer setAccounts() method:. c1.setAccou none none nts(accounts1); [ 72 ]. 4 . In this cas none none e customer c1 has only one account, a1. If a customer has more than one account then each account would be added to the above ArrayList. We handle addresses in a similar manner to accounts.

Then we can persist our customer object. The findCustomer() implementation simply uses the entity manager find() method that we have seen in the previous chapter:. public Cust omer findCustomer(int custId) { return ((Customer) em.find(Customer.class, custId)); }.

The followi none none ng code fragment shows how we might invoke the above methods from a client, BankClient:. custId = In teger.parseInt(args[0]); bank.createCustomers(); Customer cust = bank.

findCustomer(custId); System.out.println(cust);.

We need to none for none modify the build.xml file to include passing of an argument to BankClient. For example:.

<target name="run-client"> <exec executable="${glassfish.home}/bin/appclient" failonerror="true" vmlauncher="false"> <arg line="-client ${glassfish.home}/domains/domain1/generated/xml/ j2ee-apps/BankService/BankServiceClient.

jar -mainclass ejb30.client.BankClient 4"/> </exec> </target>.

If we run t none none he client we get the following output:. run-client: [java] [Customer Id =4,first name=EDWARD,last name=COOK, referee=[referee id=4,name=RICHARD BRANSON, comments=HE SHOULD BANK WITH US], addresses={IndirectList: not instantiated}, accounts={IndirectList: not instantiated}]. [ 73 ]. Object/Relational Mapping Note that, none none null values are printed out for addresses and accounts. This is because the default is to lazily load associated entities in a one-to-many and many-tomany relationship. By this we mean that when we load a Customer object from the database we do not automatically load associated Account and Address objects.

We will discuss this in more detail later in this chapter.. O/R Mapping Overriding Defaults In this sec none for none tion we modify our object model and make it bidirectional. We will also take the opportunity to override some mapping defaults. Shown below is a UML diagram for the bidirectional model.

. A Customer entity can now be referenced by Account and Address entities. However, we still leave the Customer and Referee relationship unidirectional; we have no need in our application to reference a Customer entity from a Referee entity..

[ 74 ]. 4 . Customer Entity First we mo dify the Customer entity; the listing is shown below with modifications highlighted. We will discuss these modifications in detail..

@Entity pub none for none lic class Customer implements Serializable { private int id; private String firstName; private String lastName; private Referee referee; private Collection<Address> addresses; private Collection<Account> accounts; public Customer() {} @Id @Column(name="CUSTOMER_ID") public int getId() { return id; } public void setId(int id) { = id; } @Column(name="FIRST_NAME", length=30) public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } @Column(name="LAST_NAME", length=30) public String getLastName() { return lastName; } public void setLastName(String lastName) { this.

lastName = lastName; } @OneToOne public Referee getReferee() { return referee; } public void setReferee(Referee referee) { this.referee = referee; } @OneToMany(mappedBy="customer", fetch=EAGER) public Collection<Account> getAccounts() { return accounts; } public void setAccounts(Collection<Account> accounts) { this.accounts = accounts; } @ManyToMany(fetch=EAGER) @JoinTable( name="CUSTOMER_ADDRESS", joinColumns=@JoinColumn( name="CUST_ID", referencedColumnName="CUSTOMER_ID"), inverseJoinColumns=@JoinColumn( name="ADD_ID", referencedColumnName="ADDRESS_ID") [ 75 ].

Object/Rela tional Mapping ) public Collection<Address> getAddresses() { return addresses; } public void setAddresses(Collection<Address> addresses) { this.addresses = addresses; } public String toString() { return "[Customer Id =" + id + ",first name=" + firstName + ",last name=" + lastName + ", referee=" + referee + ",addresses=" + addresses + ",accounts=" + accounts + "]"; } }. To override none for none the default column name, use the name element of the @Column annotation. The following code fragment specifies the column name for the id field to be CUSTOMER_ID:. @Column(nam e="CUSTOMER_ID") public int getId() { return id; }. We will nam none for none e primary key columns for all our entities according to the format ENTITY + underscore + ID. Recall the Derby database default column length for String fields is 255. To override this use the length element of the @Column annotation.

The following renames the column name for the firstName field and gives a length of 30:. @Column(nam e="FIRST_NAME", length=30) public String getFirstName() { return firstName; }. Next we exa none none mine the one-to-many relationship between Customer and Account. The relationship is now bidirectional, the many side of a bidirectional one-to-many relationship is always the owning side. So Account is the owning side.

In the inverse side of a bidirectional, one-to-many relationship we need to specify the owning field using the mappedBy element. The customer field in the owner Account entity references the inverse Customer entity relationship. The resulting @OneToMany annotation will be:.

@OneToMany( mappedBy="customer") public Collection<Account> getAccounts() { return accounts; }. [ 76 ].
Copyright © . All rights reserved.