View Full Version : Let's make a poll
Arno Werr
Jan 18th, 2006, 11:55 AM
http://scosoft.com/s/y/4a1e583f.gifLet's make a poll.
When you generate Hibernate artifacts in Spring or without Spring what is your favorite strategy:
a. hand-craft *.hbm.xml files and then using hbm2java task generate *.java files (POJOs)
b. hand-craft *.java files (POJOs) with XDoclet hibernate annotations and then generate *.hbm.xml files and hibernate.cfg.xml
How would you explain the benefits of your favorite strategy over the alternative?
Cheers,
Arno
cosmin
Jan 18th, 2006, 01:14 PM
I've allwais used hbm.xml and generated the .java files .
I can see benefits of generating the hbm.xml files thogh ...
Allthough it is probably bad practice , one can run , if there's ever need , any logic in the getters or setters of the java data objects . . .
I needed to do this twice so far , and both times it was because i didn't know how to use hibernate properly :)
Arno Werr
Jan 18th, 2006, 01:52 PM
Me too.
Firstly, *.hbm.xml using help from dtd and EDI auto completion features and then generate *.java. I feel like I am more in the control of the outcome in this scenario.
Who else? What's your favorite strategy or strategy applied at your workplace?
jwray
Jan 18th, 2006, 04:55 PM
Personally, I haven't touched a hbm file in months and I like it like that.
I generate my Java files by hand and annotate them using the Java 5 annotations. Before that I used xdoclet. Since I have to edit the java files anyway to add other logic I've found I prefer this way - all editing is done in one file. Annotations really reduce the chances of type or name mismatch between the files.
Jonny
cosmin
Jan 19th, 2006, 08:36 AM
how do you use anotations for this , have you written your own code to generate the hbm.xml file ?
Arno Werr
Jan 19th, 2006, 09:20 AM
I do second this question. Jonny, can you provide a code sample of a simple domain object with Java 5 annotations for Hibernate?
cosmin
Jan 19th, 2006, 11:09 AM
oh wait come to think of it , i did see Hibernate Annotations on the hibernate page , maybe they've just provided their own annotations for this , makes sense ...
question still stands as i'm booked for another 3 days untill i get time to research this myself :D
jwray
Jan 19th, 2006, 11:31 AM
Cosmin,
You did indeed answer your own question. Hibernate has (a currently beta) project that provides an annotations based way of doing the mappings. No hbm files are ever generated, there's a specific session factory implementation that constructs itself based on the annotations. Here's a simple class for illustration
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratorType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.apache.commons.lang.StringUtils;
import net.fiveprime.domainmodel3.analysis.VectorAnalyzer ;
/**
* Representation of a probe on an Affy gene chip
*
* @author Jonny Wray
*
*/
@Entity()
@org.hibernate.annotations.Entity(mutable=false)
@Table(name = "probe")
public class AffyProbe implements Serializable {
private static final long serialVersionUID = 3258408447950467639L;
public static final AffyProbe NULL_OBJECT = new AffyProbe();
private String probeId = "";
private Set<AffyChip> chips = Collections.emptySet();
private Set<ChipExpression> bodyMap = Collections.emptySet();
private Set<AffyProbeMapping> probeMappings = Collections.emptySet();
@Id(generate=GeneratorType.NONE)
@Column(name="probe_id")
public String getId() {
return probeId;
}
public void setId(String id) {
this.probeId = id;
}
@OneToMany(mappedBy="affyProbe", fetch=FetchType.LAZY)
public Set<AffyProbeMapping> getProbeMappings(){
return probeMappings;
}
public void setProbeMappings(Set<AffyProbeMapping> probeMappings){
this.probeMappings = probeMappings;
}
@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(
table=@Table(name="probe_on_chip"),
joinColumns={@JoinColumn(name="probe_id")},
inverseJoinColumns={@JoinColumn(name="chip_id")})
public Set<AffyChip> getChips() {
return chips;
}
public void setChips(Set<AffyChip> chips) {
this.chips = chips;
}
@Override
public String toString(){
return probeId;
}
@OneToMany(mappedBy="affyProbe", fetch=FetchType.LAZY)
public Set<ChipExpression> getBodyMap() {
return bodyMap;
}
public void setBodyMap(Set<ChipExpression> bodyMap) {
this.bodyMap = bodyMap;
}
@Transient
public String getProteinAnnotation(){
Set<String> annotations = new HashSet<String>();
for(AffyProbeMapping mapping: probeMappings){
annotations.add(mapping.getNrProtein().getAnnotati on());
}
return StringUtils.join(annotations.toArray(), "; ");
}
/**
* natural order is by probe id
*/
public int compareTo(Object o){
AffyProbe castObj = (AffyProbe)o;
return getId().compareTo(castObj.getId());
}
}
In fact, the annotations are an implementation of those from the upcoming EJB3 persistance specification. For most mappings you can be completely independent of hibernate. But, they do have some hibernate specific extensions that can be useful.
Also, so of the more esoteric mapping constructs maybe not supported. I haven't had a problem personally though. But, the new session factory allows you to mix and match annotations and hbm files. For example, when I was converting my classes from using the xdoclet/hbm approach to annotations I could convert a class at a time by using hbm files for the unconverted classes. So, if there's a mapping construct that's not yet supported by the annotations you can always fall back on using a hbm file for that specific class.
Jonny
cosmin
Jan 19th, 2006, 04:29 PM
Cool ...
allthough i'm all about hbm.xml's now , I think that's definently the way to go for my next proj .
Colin Yates
Jan 30th, 2006, 04:06 AM
I always roll my own (both Java and mapping files).
It is the only way I feel truly comfortable that I know what is going on :)
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.