320 words

Conditional Selection

2017-07-26 20:05

I have an Employee class that holds...

private String firstName = "-";
private String middleName = "-";
private String lastName = "-";

I do not require that an Employee's middle name be provided so when I use...

SELECT firstName + " " + middleName + " " + lastName FROM Employee
    ORDER BY lastname, firstname, middleName

I may end up with the result list of...

Jane K Doe
John - Doe
Jacob B Smith

I'd prefer if I could have something similar to this in Query form...

ArrayList<String> list = new ArrayList<String>();
for(Employee emp : Employee.allEmployees())
        list.add(emp.getFirstName() + " " + emp.getLastName());
alphabetizeList(list);  //lastName, firstName, middleName

The results in list will be...

Jane K Doe
John - Doe
Jacob B Smith

Can this same result list be constructed purely using an ObjectDB Query?

Thank you,

DragonRulerX's picture
Joined on 2017-07-26
User Post #1
2017-07-27 11:06

Possible solutions:

  1. Use a result class that will receive firstName, middleName and lastName and will have a toString() implementation that composes the correct name.
  2. Use a report query that returns firstName, middleName and lastName as separate fields and then process the results for presentation in your code, after getting the query results.
  3. Maintain an additional field. e.g. displayName.
  4. Use a method in the SELECT clause. It could be a static method that receives firstName, middleName and lastName and returns a string for display or an Employee method (e.g. toString). If client-server mode is used then the method must be available also on the server side (by running the server with the correct classpath).
ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
support's picture
Joined on 2010-05-03
User Post #2,785
2017-07-27 19:45

Interesting solutions. I've chosen to use the additional field option as it should only contribute an extra 20 - 24 bytes on average per entry to the database given what I have to work with. However, I did note your first solution of the result class as a way to allow java garbage collection to remove these objects when the program terminates so as to not persist them unless necessary.

Thank you for the suggestions,

DragonRulerX's picture
Joined on 2017-07-26
User Post #2

Post Reply

Please read carefully the posting instructions - before posting to the ObjectDB website.

  • You may have to disable pop up blocking in order to use the toolbar (e.g. in Chrome).
  • Use ctrl + right click to open the browser context menu in the editing area (e.g. for using a browser spell checker).
  • To insert formatted lines (e.g. Java code, stack trace) - select a style in the toolbar and then insert the text in the new created block.
  • Avoid overflow of published source code examples by breaking long lines.
  • You may mark in paragraph code words (e.g. class names) with the code style (can be applied by ctrl + D).
  • Long stack traces (> 50 lines) and complex source examples (> 100 lines) should be posted as attachments.
Maximum file size: 32 MB