简体   繁体   English

休眠-一对多关联

[英]Hibernate - One to Many associations

i have two tables, ComputerNode and Connection. 我有两个表,ComputerNode和连接。 ComputerNode has a primary key nodeid but Connection do not have one. ComputerNode有一个主键nodeid但是Connection没有一个。 I can't modify the table schema. 我无法修改表架构。 How should i create the java POJO if they were to have one-to-many relationship? 如果它们具有一对多关系,我应该如何创建Java POJO? 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. 这是SQL表架构。 ComputerNode table: ComputerNode表:

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. 我创建了两个Java POJO类,但是我不确定所需的注释。 I have read the hibernate tutorial but i don't see explanation on class without a identifier(ie: Connection). 我已经阅读了休眠教程,但是没有标识符(即:Connection),我看不到有关类的说明。

ComputerNode.java: 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 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? Connection应该是哪种类型的类? @Embeddable? @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());
 }
}

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 如果连接表没有任何主键,请检查以下解决方案: 休眠和不带PK的表

=========== ===========

Updated 更新

If you want to select ComputerNode entities use this query: sessionFactory.getCurrentSession().createQuery("select node from ComputerNode as node").list(); 如果要选择ComputerNode实体,请使用以下查询: sessionFactory.getCurrentSession().createQuery("select node from ComputerNode as node").list();

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 您还必须使Connection类也成为一个实体,因为要在两个实体类之间建立关系,只需将@Entity放在它上面

@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. 希望这会帮助你。

I'm not really sure what you have to do without id . 我不确定自己没有id怎么办。

Maybe this can help... 也许这可以帮助...

@OneToMany (cascade = {CascadeType.ALL}) 
@JoinTable(
    name="Connection",
    joinColumns = @JoinColumn( name="nodeid")
)
private Set<Connection> connections;

best! 最好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM