Friday, February 27, 2015

Hibernate's inverse=true on sets, and cascade=all

review this example: http://www.mkyong.com/hibernate/inverse-true-example-and-explanation/

upshot: makes more sense to do inverse=true -- results in fewer SQL statements.

But what if you just want a many-to-one relationship? e.g. a Rectangle that has a instance variable of type Point and Point can be a property of multiple Rectangles. Furthermore, you only need this to be unidirectional. No need to navigate from Point to Rectangles. (Note: a rectangle's location be defined by a single point.) In this case inverse="true" won't help because it can only be applied to a collection not a property. So what if you want to persist the Rectangle with one Hibernate save statement, and not have to first save the point, then save the rectangle to avoid a referential integrity error. The trick was mentioned in passing here: using cascade="all" on the many-to-one element in the unidirectional relationship's only side, in this case Rectangle, or if using annotations @ManyToOne(cascade=CascadeType.ALL) -- with cascade set to all, " if we perform an insert on the child row, Hibernate will automatically create the parent row based on where the child row is pointing." THAT saves a line of code, which can be a HUGE benefit especially if this is a polymorphic Hibernate util, otherwise, you would have to do something like either cast the entity to see if it's a Rectangle and has a point or in which case persist the Point or create a "performPreSave(session)" which is called on all entities where a Rectangle could override and persist its Point. Hacky! So...use cascade="all"...

Update. Rather than cascade="all" to achieve this single-save-cascade effect, should use "save-update" because you don't want to cascade a delete probably ever, if it's a many-to-one, since there could be other shapes like polygon relating to that point and possibly even other rectangles. In fact, cascade delete would probably only ever make sense in a one-to-one, and only ever in a one-to-many if the many-side table (the table holding the foreign key to the one-side table) did not contain foreign key references / relations to other entities. Answer here referencing Hibernate docs agrees with this.

This save-update effect would be useful to the afore-mentioned unidirectional relationship for the purposes of adding the related records upon saving the primary record, in this case the rectangle.

Saturday, December 27, 2014

Observable: Reason for setChanged()

This is useful because it separates the part where you say that the Observable has changed, from the part where you notify the changes. (E.g. Its useful if you have multiple changes happening and you only want to notify at the end of the process rather than at each small step). This is done through setChanged().
- jbx http://stackoverflow.com/questions/13744450/interview-when-do-we-use-observer-and-observable

 ...setChanged is used like a flag I think. This is to avoid unnecessary updating
- James Poulson http://stackoverflow.com/questions/7271044/observable-in-java

But note possible race condition in Java's implementation of Observable where a second call to notifyObservers may not be completed and thus second parameter (if one is provided) is not sent. See answer by mab at http://stackoverflow.com/questions/4446718/why-observable-snapshot-observer-vector/11122611#11122611.

Saturday, December 13, 2014

How to specify JAXB root element

With no metadata specified we need to supply JAXB with a root element name (and namespace).
http://blog.bdoughan.com/2010/10/how-does-jaxb-compare-to-xstream.html
@XmlRootElement
public class Customer {
...

Friday, October 10, 2014

Iteration Syntaxes

http://www.javaworld.com/article/2461744/java-language/java-language-iterating-over-collections-in-java-8.html

Do each syntax on demand.

Sunday, October 5, 2014

Uses of targetNamespace

Example:
http://www.liquid-technologies.com/Tutorials/XmlSchemas/XsdTutorial_04.aspx

Ex. how to define in JAXB @XmlSchema annotation:
http://stackoverflow.com/questions/16584555/understanding-jaxb-xmlrootelement-annotation

Monday, September 29, 2014

Java wait() releases synchronization lock

Threads calling wait() release the synchronization lock on the current instance, i.e. the method with synchronized keyword is allowed to be entered by other threads.
http://tutorials.jenkov.com/java-concurrency/starvation-and-fairness.html