我有以下两个实体,但在
Trainer
的 @OneToMany
关系行中,它显示错误消息“无法找到连接表 trainer_batch”,而我正在使用 @JoinColumn
注释。这是我的java代码:
@Entity
@Table(name="trainer")
public class Trainer {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
int id;
String name;
@OneToMany(cascade=CascadeType.ALL) // this is the line
@JoinColumn(name="trainerid")
Set<Batch> batches=new HashSet<Batch>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Batch> getBatches() {
return batches;
}
public void setBatches(Batch batch) {
batches.add(batch);
}
}
@Entity
@Table(name="batch")
public class Batch {
public String getSlot() {
return slot;
}
public void setSlot(String slot) {
this.slot = slot;
}
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic;
}
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
int id;
String slot;
String topic;
}
persistance.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="one_to_many_pk_fkPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name = "hibernate.connection.driver_class" value = "com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name = "hibernate.connection.url" value = "jdbc:sqlserver://localhost:1433;DatabaseName=test; MARS_Connection=yes;"/>
<property name = "hibernate.connection.username" value = "sa"/>
<property name = "hibernate.connection.password" value = "test"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
</properties>
</persistence-unit>
</persistence>
您应该使用通用作为 Batch 。
像这样使用你的关系:
Set