Spring with Hibernate, join data from another table and map to a new helper object
first i have to say i am quite new with spring and hibernate.
now my situation: i have one table named Places which has: Id PK, name and description. i have a second table named Edges which has: ID PK, fromPlaces (fk to the pk id from places), and toPlaces (also referenced to the id from places).
now i want to make a query which will eventually map to an object which will have my Edge object plus both the names of the fromPlace reference and toPlaces reference.
currently i have this in my DAO which is functioning properly:
public List<Edge> listEdges() {
return sessionFactory.getCurrentSession().createQuery("from Edge")
.list();
}
Please any good tutorial or any code examples you can give me to help me with the process of building what i need. i dont know if its helpful but i am using annotations to wire everything.
SECOND EDIT: ADding the solution to my classes for both referencing and afterwards if a Place is removed to automatically remove the references to Edge objects.
@Entity
@Table(name="PLACES")
public class Place {
@Id
@Column(name="ID")
@GeneratedValue
private Integer id;
@Column(name="NAME")
private String name;
@Column(name="DESCRIPTION")
private String description;
@Column(name="IMAGEURL")
private String imageUrl;
@Column(name="ISVALIDDESTINATION")
@Type(type = "org.hibernate.type.NumericBooleanType")
private boolean validDestination;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "FROMPLACE_ID")
private Set<Edge> fromPlace = new HashSet<Edge>(0);
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "TOPLACE_ID")
private Set<Edge开发者_StackOverflow社区> toPlace = new HashSet<Edge>(0);
And this is my edge model: note i am ommiting the getters and setters here because they have no annotations,they are just standard.
@Entity
@Table(name = "EDGES")
public class Edge {
@Id
@Column(name = "ID")
@GeneratedValue
private Integer id;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="FROMPLACE_ID")
private Place fromPlace;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="TOPLACE_ID")
private Place toPlace;
@Column(name="COST")
private Integer cost;
First of, you need to map your entities. "Hibernate Annotations" docs are a good place to start.
Links from Edge
to Places
(BTW, naming your entities in plural is confusing; I think that's why duffymo suggested collections) should be mapped as @ManyToOne
Once you do that, your listEdges
method above would return a list of Edge
objects, each of which would have (possibly lazily populated) getFromPlaces()
and getToPlaces()
methods returning appropriate Places
instances.
Now, if you want to return a separate helper object that contains Edge
and only names of relevant Places
entities, you'll have to create that object along with appropriate constructor and write a query referencing that constructor.
This is a bit more advanced, though - I'd recommend you get the first part working and look into working with lazy associations before you proceed with this.
Edit (based on question update):
Your Edge
mapping should instead be something like:
@Entity
@Table(name = "EDGES")
public class Edge {
@Id
@GeneratedValue
private Integer id;
@ManyToOne
@JoinColumn(name="FROMPLACE")
private Place fromPlace;
@ManyToOne
@JoinColumn(name="TOPLACE")
private Place toPlace;
}
Note that it's not necessary to specify @Column
on fields whose name matches column name. Also note that it's common to use "_ID" suffix to indicate foreign keys (e.g. "FROMPLACE_ID") - if you do that, you can omit @JoinColumn
declarations as well.
Stop thinking less in terms of tables and more in terms of objects.
You need an Edge object with 1:m relationships to Place references, to and from.
精彩评论