Archive for July, 2009
Hibernate’s @FilterJoinTable
Couldn’t find a completely articulate example or full documentation of how to add a filter to a Hibernate Many-to-Many collection association mapping, so thought I’d would post a complete example here.
It’s possible to apply the filter to the target entity table which is reasonably well documented, or at least you can have an educated guess, but also to the association table of the Many-to-Many relationship where the docs were a little less clear.
I have the following relational association table:
CREATE TABLE departure_point_resort_association (
departure_point_id VARCHAR(36) NOT NULL,
resort_id VARCHAR(36) NOT NULL,
site_id ENUM('30', '31'...) NOT NULL,
PRIMARY KEY (departure_point_id, resort_id),
CONSTRAINT assoc_departure_point_fk FOREIGN KEY (departure_point_id)
REFERENCES departure_point (id),
CONSTRAINT assoc_resort_fk FOREIGN KEY (resort_id) REFERENCES resort (id)
);
which is mapping a ski resort to potential departure points or airports. There was no real requirement to persist an actual SITE table (which would be entirely static data) and to map to it’s foreign key value, so instead am using a simple MySQL ENUM column above, and wanted to apply a filter at the application level to the collection of Resorts based on this site_id.
@Entity @Table
@FilterDef(name = "siteFilter", parameters = {
@ParamDef(name = "siteId", type = "string")
})
public final class Resort extends BasePersistentObject {
@ManyToMany(fetch = FetchType.EAGER)
@FilterJoinTable(name = "siteFilter", condition = "site_id = :siteId")
@JoinTable(name = "DEPARTURE_POINT_RESORT_ASSOCIATION",
joinColumns = {
@JoinColumn(name = "RESORT_ID")
},
inverseJoinColumns = {
@JoinColumn(name = "DEPARTURE_POINT_ID")
}
)
private Set departurePoints;
Then it’s simply a case of enabling the filter in the client (DAO or whatever your framework dictates):
session.enableFilter("siteFilter").setParameter("siteId", String.valueOf(siteId));
Programming for grown-ups
I sent an article by Ted Dziuba of The Register around to my development team earlier this morning. As well as some well-deserved shots at Microsoft, it contained a rather acidic commentary (not like him) on the merits of the PHP programming language. Or rather lack of.
One of my colleagues replied with the following:
Hibernate StatelessSession with Transactional Annotation
I have a bi-directional collection mapping in Java which I thought was causing me problems when persisting using Hibernate’s StatelessSession API.
However, it looks as though the issue was being caused by the fact I was using the @Transactional annotation instead. The API states:
“Operations performed via a stateless session bypass Hibernate’s event model and interceptors.”
I didn’t pick up on the importance of the above statement as it’s not particularly clear at first glance, but it looks as though using Annotations via AOP just isn’t supported when streaming directly to the database using the StatelessSession.
Commenting out the annotation and using a programmatic Transaction instead corrected the problem:
// @Transactional
public List<Package> insertAvailability(final List<Package> packages) {
final StatelessSession session = sessionFactory.openStatelessSession();
final Transaction tx = session.beginTransaction();
session.insert(aPackage);
// insert collections manually etc...
tx.commit();
session.close();
return packages;
}
Agile CTO
- tech_startup: Puppet certificate issues http://t.co/oM6y53rx 4 days ago
- have invented a new UNIX tool for cutting the grass: sudo chmown aeells:aeells squid.conf.bkp sudo: chmown: command not found 5 days ago
- I support #wikipediablackout Show your support here http://t.co/UFN8O0gk 2 weeks ago
- reasonable man adapts himself to world; unreasonable man tries to adapt world to himself; => all progress depends on unreasonable man. 3 weeks ago
- @blinkdesign we could do that too!!!!? ;o) in reply to blinkdesign 3 weeks ago
- @blinkdesign we could do something similar on our tech blog maybe... in reply to blinkdesign 3 weeks ago
- ".....in all things, the supreme excellence is simplicity." Henry Wadsworth Longfellow 3 weeks ago
- good day bootstrapping #tomcat #memcached to #aws server via #puppet - looking forward to provisioning entire production replica in minutes! 3 weeks ago
- great (practical) example of how to do #continousdeployment and branching within teams http://t.co/ceeyvD0h courtesy @chacon @domfarr 2012-01-05
- hi @ruv, mind if i ask how you came by that statistic? cheers! in reply to ruv 2012-01-02
- More updates...


