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?
================
Update
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());
}
}