【问题标题】:Foreign Key of Type String in HibernateHibernate中字符串类型的外键
【发布时间】:2019-10-02 04:32:40
【问题描述】:

我正在使用 Spring Boot 来开发 Hibernate-JPA。我的外键是字符串类型。在Junit测试后检查MYSQL数据库时,我注意到数据库中的外键字段为空

我的代码如下:

儿童班:

@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;
    }
}

父类:

    @Entity @Table(name = "node_types")
    public class Nodetypes {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String label;
    private String name;
    private Boolean is_group;
    private Date created_at;
    private String type;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @OneToMany(mappedBy = "nodetypes", cascade = CascadeType.ALL)
    private Set<Nodes> nodes;

    public Set<Nodes> getNodes() {
        return nodes;
    }

    public void setNodes(Set<Nodes> nodes) {
        this.nodes = nodes;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Boolean getIs_group() {
        return is_group;
    }

    public void setIs_group(Boolean is_group) {
        this.is_group = is_group;
    }

    public Date getCreated_at() {
        return created_at;
    }

    public void setCreated_at(Date created_at) {
        this.created_at = created_at;
}

我的测试条件是为 MYSQL 数据库中的外键生成一个空字段

    @Test
    public void testCreateNodetype() {
        Nodetypes nodetypes = new Nodetypes();
        nodetypes.setId(1);
        nodetypes.setLabel("myLabel");
        nodetypes.setName(44);
        nodetypes.setIs_group(true);
        nodetypes.setCreated_at(new java.util.Date());

        nodetypesRepository.save(nodetypes);
    }

    @Test
    public void testCreateNodes() {
        Nodes node1 = new Nodes();
        node1.setCreated_at(new java.util.Date());
        node1.setName("nodeName");
        node1.setNode_id(444);
        node1.setNodetypes(nodetypesRepository.findOne(1));

        nodesRepository.save(node1);
    }

这里是使用的 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

【问题讨论】:

  • 你能分享一下简约表格架构吗
  • 是的,我有。请看我的问题
  • (name="type", insertable=false, updatable=false) 你在这里看到了什么?
  • 习惯上说“type”字段是外键
  • 但是如果你指定insertable=false,为什么你会奇怪没有插入一个字段呢?

标签: java hibernate spring-boot jpa hibernate-mapping


【解决方案1】:

发现以下类的实体建模存在一个问题

CREATE TABLE `node_types` (
  `name` varchar(255) NOT NULL COMMENT 'unique identification'
@Entity @Table(name = "node_types")
    public class Nodetypes {
    private int name;

将名称的返回类型从 int 更改为 String

@Entity @Table(name = "node_types")
    public class Nodetypes {
    private String name;

把测试方法也改成

    @Test
    public void testCreateNodetype() {
      ...
        nodetypes.setName("44");
      ... 
    }

这应该可以正常工作。

以下是表格中的条目:

【讨论】:

  • 我按照你的建议做了。我仍然在数据库中得到一个空值
  • 还有一些其他类,请忽略它们,只考虑必需的:github.com/dineshbhagat/spring-boot-web-jpa/tree/master/src/…
  • 克隆完整项目,您可以运行 JUnit,这将在两个表中创建条目
  • 我应该下载整个项目吗?
  • 好的,我试试。给我几分钟
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-01
相关资源
最近更新 更多