August 26, 08 by Bryan
This took me a few hours to figure out so I’m logging this here for my own sanity. If you have a bidirectional many-to-many mapping in Hibernate with a join table, the join table is only updated when the noninverse side of the mapping is inserted/updated. In the example below, the join table (pane_specs) is only updated when a spec is saved, not when a pane is saved:
Spec.java
@ManyToMany
@JoinTable(
name = “pane_specs”,
joinColumns = { @JoinColumn(name = “spec_id”) },
inverseJoinColumns = { @JoinColumn(name = “pane_id”) }
)
public List<Pane> getPanes() {
Pane.java
@ManyToMany(mappedBy = “panes”)
public List<Spec> getSpecs() {
May 16, 08 by Bryan
A coworker and I were tasked with rewriting an internal application to manage our company’s workflow. We both have a background in Perl and not much Java, making us the perfect candidates to prototype potential frameworks. The following is what we came up with based on our (few) requirements:
- MVC framework
- view layer must allow us to write html and css
- must be flexible enough to extend to other upcoming projects
The final decision ended up being struts2, mainly due to it’s flexibility. Other frameworks seemed like they would allow us to develop more quickly, but we would have been stuck with them (custom tags, combined view/controller layers, etc). With struts and hibernate for the model layer, it seemed like we could quickly move to something else easily.
Servlets/JSP/JSTL
- Pros:
- Not a lot of re-learning, everything can be done pretty much the same way as in CGI.
- We have (read: Roger has) JSP and JSTL books
- Cons:
- Requires a couple little helper classes for working with Hibernate
- JSTL isn’t especially graceful
- Doesn’t look good on a job description or resume
Tapestry
(I tested 4.1, the current GA release. 5.0 is in beta testing, and probably will be released before long. There are already books out for 5, but not really any online tutorials.)
- Home page: http://tapestry.apache.org/
- Lots of tutorials: http://tapestry.apache.org/tapestry4.1/tutorials/index.html
- Hibernate+Tapestry: http://www.hibernate.org/96.html
- Pros:
- Allows you to write HTML with only a couple of additional non-standard attributes
- Very well documented and tutorial-ed.
- One “page specification” XML file (*.page) per page, instead of a single huge XML config file
- Coding is extremely simple, most of the complexity is in the page files.
- Cons:
- Lots of different components (for page specification files) using a kind of non-standard language
- Notorious for changing API between major releases (i.e. 5.0).
Wicket
- Home page: http://wicket.apache.org/
- A dated tutorial: http://cwiki.apache.org/WICKET/newuserguide.html
- Databinder, a Wicket+Hibernate linking library: http://databinder.net/site/show/overview
- Pros:
- An Apache project, implying longevity or better integration going forward
- Lots of people think it represents the future of Java frameworks
- AJAX and jQuery integration built-in
- Cons:
- Uses a very unique design pattern, a huge rethink from standard CGI or servlet coding.
- Tutorials and other information is a bit dated, making it hard to learn, especially with the unique design pattern. (This is even more true for Databinder)
- Lots of the HTML can/should/must be generated by code, instead of by hand.
Struts
- Home page: http://struts.apache.org/
- Tutorial: http://struts.apache.org/2.x/docs/hello-world.html
- Pros:
- Fairly minimal. Struts does some things for you, but keeps the general Servlet/JSP structure more or less unchanged
- Integration with Hibernate is as simple as with Servlets/JSP
- Virtually ubiquitous
- eCommerce suite uses Struts, so we will have to know it anyhow
- Cons:
- Eats babies
- Tastes like dirt
JSF (Java Server Faces)
GWT (Google Web Toolkit)
Echo
March 17, 08 by Bryan
These are some notes I scribbled down during Sun’s “Java Programming” class. Having programmed in Perl the last 7 years, this is what I wrote down as “noteworthy.”
- Everything in Java is an object except primitive types
- All objects are references in memory
- A ‘foreach’ loop in java looks like this:
for (Car y: x) {
y.speed = 100;
}
- Inherited objects always call the super classes constructor. The default is super() with no arguments. This can be overridden by placing a super(int i) as the FIRST EXECUTABLE STATEMENT in a subclass constructor.
- All objects inherit from the base class ‘Object’.
- obj instanceof class will return true or false based on whether obj is indeed an object of type class.
- defining the toString method for object obj allows you to define what is printed when System.out.println(obj) is called.
- The static keyword declares members that are associated with the class and not the class objects
- The final keyword ensures a member can not be changed. This means that methods and classes that use final can not be overridden or extended.
- The abstract keyword:
- forces child classes to define a method.
- classes with an abstract method must also be abstract.
- abstract classes can not be instantiated directly (only via subclasses)
- An interface is a class with only abstract methods
- The implements class keyword is used for interfaces to enforce the rules of the interface. Unlike extends, implements can be used on more than one interface.
- For handling exceptions, you can use try and catch blocks, or declare your method with a ‘throws‘ clause that sends the exception back to whatever called that method.