Reputation: 4979
i have two tables, ComputerNode and Connection. ComputerNode has a primary key nodeid
but Connection do not have one. I can't modify the table schema. How should i create the java POJO if they were to have one-to-many relationship? basically my objective is to do a inner join like this:
select * from `ComputerNode` cn inner join `Connection` c on cn.nodeid = c.nodeid
Here are the SQL table schema. ComputerNode table:
int nodeid <primary key>;
varchar nodename;
Connection table:
int nodeid <not primary key>;
varchar connstatus;
The relationship between the tables is one-to-many. A computer node can have many connections
I have created two Java POJO classes but i'm not exactly sure about the annotations required. I have read the hibernate tutorial but i don't see explanation on class without a identifier(ie: Connection).
ComputerNode.java:
@Entity
@Table(name="ComputerNodes")
public class ComputerNode {
@Id
@Column(name="nodeid")
private int nodeId;
@Column(name="nodename")
private String nodeName;
@OneToMany
private Set<Connection> connections;
.... //getter and setters
}
Connection.java
//What annotation should i use since this class doesn't have identifier?
public class Connection {
@Column(name="nodeid")
private int nodeId;
@Column(name="connstatus")
private String connStatus;
}
What type of class is Connection supposed to be? @Embeddable? What should I do to create a one-to-many relationship between the two classes?
================
public List<ComputerNode> getComputerNodes() {
//the query to inner join is:
return sessionFactory.getCurrentSession().createQuery("from ComputerNode as node inner join Connection as conn").list();
}
for (ComputerNode cn : getComputerNodes) {
System.out.println(cn.getNodeId() + ',' + cn.getNodeName());
for (Connection c : cn.getConnections) {
System.out.println(c.getConnStatus());
}
}
Upvotes: 1
Views: 150
Reputation: 517
Try this:
@Entity
@Table(name="ComputerNode")
public class ComputerNode {
@Id
@Column(name="nodeid")
private int nodeId;
@Column(name="nodename")
private String nodeName;
@OneToMany(mappedBy="computerNode")
private Set<Connection> connections;
.... //getter and setters
}
Connections:
@Entity
@Table(name="Connection")
public class Connection {
@ManyToOne
@JoinColumn(name="nodeid")
private ComputerNode computerNode;
...
}
If Connection table doesn't have any primary key, check this solution: Hibernate and table without PK
===========
Updated
If you want to select ComputerNode entities use this query:
sessionFactory.getCurrentSession().createQuery("select node from ComputerNode as node").list();
Upvotes: 1
Reputation: 26961
I'm not really sure what you have to do without id
.
Maybe this can help...
@OneToMany (cascade = {CascadeType.ALL})
@JoinTable(
name="Connection",
joinColumns = @JoinColumn( name="nodeid")
)
private Set<Connection> connections;
best!
Upvotes: 0
Reputation: 451
You have to make the Connection class to become an entity also, for you to have a relationship between two entity class just put @Entity
on top it
@Entity
@Table(name="Connection")
public class Connection {
@ManyToOne()
//this will join the nodeId of ComputerNode entity class to Connection entity class
@JoinColumn(name="nodeId")
private ComputerNode nodeId;
@Column(name="connstatus")
private String connStatus;
}
hope this will help you.
Upvotes: 0