Java Hibernate中字符串类型的外键
我使用SpringBoot开发HibernateJPA。我的外键是字符串类型。在Junit测试之后检查MYSQL数据库时,我注意到数据库中的外键字段是空的 我的代码如下: 儿童班:Java Hibernate中字符串类型的外键,java,hibernate,spring-boot,jpa,hibernate-mapping,Java,Hibernate,Spring Boot,Jpa,Hibernate Mapping,我使用SpringBoot开发HibernateJPA。我的外键是字符串类型。在Junit测试之后检查MYSQL数据库时,我注意到数据库中的外键字段是空的 我的代码如下: 儿童班: @Entity @Table(name = "nodes") public class Nodes { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; private int node_id;
@Entity @Table(name = "nodes")
public class Nodes {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private int node_id;
private String name;
private Date created_at;
@ManyToOne
@JoinColumn(name="type", insertable=false, updatable=false)
private Nodetypes nodetypes;
@OneToMany(mappedBy = "nodes", cascade = CascadeType.ALL)
private Set <Nodeattributes> nodeattributes;
@OneToMany(mappedBy = "nodes", cascade = CascadeType.ALL)
private Set <Products> products;
public Set<Products> getProducts() {
return products;
}
public void setProducts(Set<Products> products) {
this.products = products;
}
public Set<Nodeattributes> getNodeattributes() {
return nodeattributes;
}
public void setNodeattributes(Set<Nodeattributes> nodeattributes) {
this.nodeattributes = nodeattributes;
}
public Nodetypes getNodetypes() {
return nodetypes;
}
public void setNodetypes(Nodetypes nodetypes) {
this.nodetypes = nodetypes;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getNode_id() {
return node_id;
}
public void setNode_id(int node_id) {
this.node_id = node_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreated_at() {
return created_at;
}
public void setCreated_at(Date created_at) {
this.created_at = created_at;
}
}
下面是使用的MYSql模式
这是父表:
CREATE TABLE `node_types` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`label` varchar(255) NOT NULL COMMENT 'display name',
`name` varchar(255) NOT NULL COMMENT 'unique identification',
`is_group` tinyint(1) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
)
这是子表:
CREATE TABLE `nodes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`node_id` int(11) DEFAULT NULL,
`name` varchar(255) NOT NULL,
`type` varchar(255) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `type_foreign_to_node_types` (`type`),
CONSTRAINT `type_foreign_to_node_types` FOREIGN KEY (`type`) REFERENCES `node_types` (`name`)
) ```
Any help would be apreciated
发现以下类的实体建模存在一个问题
CREATE TABLE `node_types` (
`name` varchar(255) NOT NULL COMMENT 'unique identification'
将名称的重新返回类型从int更改为String
@Entity @Table(name = "node_types")
public class Nodetypes {
private String name;
将测试方法也更改为
@Test
public void testCreateNodetype() {
...
nodetypes.setName("44");
...
}
这应该行得通
以下是表中的条目:
你能不能也分享一下简约的表格模式?是的,我有。请看我的问题name=type,insertable=false,updateable=false你在这里看到了什么?它用来说字段类型是foriegn-key,但是如果你指定insertable=false,为什么不插入字段呢?我按照你的建议做了。我在Daabase中也得到了一个null,还有其他类,请忽略它们,只考虑需要:克隆完整的项目,并且您可以运行JUnit,它将在两个表中创建条目,我下载整个项目?好的,我会尝试。给我几分钟
@Entity @Table(name = "node_types")
public class Nodetypes {
private int name;
@Entity @Table(name = "node_types")
public class Nodetypes {
private String name;
@Test
public void testCreateNodetype() {
...
nodetypes.setName("44");
...
}