简体   繁体   中英

Hibernate insert null values ​in composite foreign key

I have two simple domain objects as follows:

USER:

@Entity
@Table(name="USER")
@IdClass(UserPK.class)
public class User implements Serializable {

    //...
    @Id
    @Column(name = "FISCALCODE")
    private String fiscalCode;

    @Id
    @Column(name = "USERNUMBER")
    private String userNumber;

    @OneToMany(mappedBy="user", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private List<Items> items;
    
    // getters and setters
}

UserPK:

public class UserPK implements Serializable {
        
  @Column(name = "FISCALCODE")
  private String fiscalCode;
    
  @Column(name = "USERNUMBER")
  private String userNumber;

  // getter and setter
}

ITEMS:

@Entity
@Table(name="ITEMS")
public class Items implements Serializable {
        
    //...
    @Id
    @Column(name = "ID_ITEM")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_item_generator")
    @SequenceGenerator( name = "id_item_generator", sequenceName = "ITEM_SEQ", allocationSize = 1)
    private Integer id;
            
    @ManyToOne
    @JoinColumns({
         @JoinColumn(name="FISCALCODE"),
         @JoinColumn(name="USERNUMBER")
    })
    private User user;
        
    // getters and setters
}

DB Table:

user { fiscalcode, usernumber, other columns... }    // fiscalcode+usernumber = PK
items { id, fiscalcode, usernumber, other columns... }  // fiscalcode,usernumber is a foreign key

CONTROLLER:  

      @RequestMapping(value="/user", method = RequestMethod.POST, produces = "application/json")
        public Object postUser(@RequestBody(required = false) User user){
        //connection etc..
        session.save(user);
        //...
        return new ResponseEntity<>(HttpStatus.OK);
      }

Why when I run the command session.save(user) Hibernate insert value null in the columns FISCALCODE and USERNUMBER of the ITEMS table?

I tried to set the ManyToOne and the JoinColumns on the getter, but the result is the same.

EDIT: i have added my method for POST operation

  1. As it's stated in the documentation for @JoinColumns :

When the JoinColumns annotation is used, both the name and the referencedColumnName elements must be specified in each such JoinColumn annotation.

So, you should correct your mapping like below:

@ManyToOne
@JoinColumns({
    @JoinColumn(name="FISCALCODE", referencedColumnName = "FISCALCODE"),
    @JoinColumn(name="USERNUMBER", referencedColumnName = "USERNUMBER")
})
private User user;
  1. Whenever a bidirectional association is formed, the application developer must make sure both sides are in-sync at all times.

So, you should have in your User entity the addItem() and removeItem() utility methods that synchronize both ends whenever a child element is added or removed like below.

@Entity
@Table(name="USER")
@IdClass(UserPK.class)
public class User implements Serializable {

    //...

    @OneToMany(mappedBy="user", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private List<Items> items;
    
    // getters and setters

   public void addItem(Items item) {
      this.items.add(item);
      item.setUser(this);
   }

   public void removeItem(Items item) {
      this.items.remove(item);
      item.setUser(null);
   }
}

Example of saving:

User user = new User();
user.setFiscalCode("Code1");
user.setUserNumber("User1");
// ...

Items item1 = new Items();
Items item2 = new Items();
// ...
      
user.addItem(item1);
user.addItem(item2);
      
session.save(user);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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