Check your spelling
Why isn’t this working?
That was my question for about a week of frustration.
Answer, I had two different variables misspelled. I won’t be making that mistace again.
tech opinions and how-tos
Archive for December 2009
Why isn’t this working?
That was my question for about a week of frustration.
Answer, I had two different variables misspelled. I won’t be making that mistace again.
So you have a relational database and you want to see what Addresses, Phone numbers, or Accounts are associated with a Person. By default, Java EE will only get the objects it absolutely needs; this is called lazy fetching. So solve this problem you want to fetch eagerly. In your code, find the attribute in your entity bean that you want to eagerly fetch and edit the attributes to indicate that it should eagerly fetch the information:
Person bean: @OneToMany(mappedBy="person", fetch=FetchType.EAGER)
private Set accounts;@OneToOne, @ManyToOne, @ManyToMany, etc. It will vary based on the circumstance.It is extremely simple to get an entity bean object from a database based on the primary key:
public Person getPerson(String personId) {
Person aPerson = manager.find(Person.class, personId);
return aPerson;
}
public Person getPerson(String userid, String password) {
if(userid == null || password == null) {
return null;
}
try {
Query q = manager.createQuery("select p from Person p where p.userid = :uid and p.password = :pass");
q.setParameter("uid", userid);
q.setParameter("pass", password);
Person aPerson = (Person) q.getSingleResult();
if(aPerson == null) {
return null;
}
return aPerson;
}
catch (Exception e) {
return null;
}
}
This is a valuable piece of code that I am sure I’ll be using in the future:
public boolean isSame(Collection expected, Collection actual) {
/* first compare the sizes, if they are not the same there is no use in going further */
if(expected.size() != actual.size()) {
return false;
}
/* the count variable will increment for each match */
int count = 0;
/* The iterators check each object in each Collection */
for (Iterator iterator = expected.iterator(); iterator.hasNext();) {
Object object = (Object) iterator.next();
for (Iterator iterator2 = actual.iterator(); iterator2.hasNext();) {
Object object2 = (Object) iterator2.next();
if(object2.equals(object)) {
count++;
}
}
}
/* the count variable is the same as the size of the Collections then all items match */
if (count != expected.size()) {
return false;
}
else {
return true;
}
}
Set was the same as an actual Set. I chose to use a Collection to make it more generic, and since a Set is a Collection it will still work.
@Stateless /*These business rules are stateless.*/
public class BusinessRules implements BusinessRulesRemote { /*class header*/
@PersistenceContext /*If you are using Java EE then you will need this to get the Persistent context*/
EntityManager manager; /*The manager you'll be using*/
public static final String REMOTEJNDINAME = BusinessRules.class.getSimpleName() + "/remote"; /*JNDI name so that the class can be found*/
public BusinessRules() { //Default constructor
}
@PersistenceUnit instead of @PersitenceContext it will not work.BusinessRules class in order to access your data. Here is an example of how to select a person using their primary key (continued from the code above):
public Person getPerson(String personId) {
Person aPerson = manager.find(Person.class, personId);
return aPerson;
}
BusinessRules class (businessRules) and then call methods (e.g. businessRuless.getPerson("1");) In order to get an Object Person as an entity from your database based on their username and password you will want to overload your method in BusinessRules (or just not use the former method since it isn’t very practical) and write a query using EJB QL. Example (continued from code above):
public Person getPerson(String userid, String password) {
Query q = manager.createQuery("select p from Person p where p.userid = :uid and p.password = :pass");
q.setParameter("uid", userid);
q.setParameter("pass", password);
Person aPerson = (Person) q.getSingleResult();
return aPerson;
}