0

I am using Spring boot for developing Hibernate-JPA. My foreign key is of type String. When I check MYSQL database after Junit tests, I notice that the foreign key field in the database is empty

My code is as below:

Child Class:

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

Parent Class:

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

My test conditions are generating an empty field for the foreign key in MYSQL Database

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

here is the MYSql schema used This is the Parent Table:

    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`)
)

This is the Child Table:

    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
16
  • 1
    can you please share minimalistic tables schema as well Commented May 15, 2019 at 5:48
  • Yes I have. Please see my question Commented May 15, 2019 at 5:54
  • (name="type", insertable=false, updatable=false) what do you see here? Commented May 15, 2019 at 6:07
  • It's used to say that the field "type" is foriegn key Commented May 15, 2019 at 6:13
  • But why do you wonder that a field is not inserted, if you specify insertable=false? Commented May 15, 2019 at 6:25

1 Answer 1

1

Found one issue with entity modeling for below class

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

change resturn type of name from int to String

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

Change the test method as well to

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

This should work fine.

Here are entries in tables:

nodetypes nodes enter image description here

Sign up to request clarification or add additional context in comments.

13 Comments

I did as you suggested. Still I'm getting a null in the daabase
There are some other class as well, please ignore them and consider only required: github.com/dineshbhagat/spring-boot-web-jpa/tree/master/src/…
clone complete project and you can run JUnit which will create entry in both the tables
should I download the whole project?
Okay.I'll try . Give me a couple of minutes
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.