简体   繁体   中英

Hibernate, error org.hibernate.util.JDBCExceptionReporter Field 'Variable_ID' doesn't have a default value

I have problem with insert data from one side relationships to other tables and to other side of relationships at the same time

I have: Table User with User Entity

@Entity
@Column(name="USER")
public class User{


private int id;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="ID")
    public int getId() {
        return id;
    }

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


@ManyToMany
@Cascade(org.hibernate.annotations.CascadeType.ALL)
@JoinTable(name="USER_CARS_JOINTABLE", 
                joinColumns=@JoinColumn(name="USER_ID"),
                inverseJoinColumns=@JoinColumn(name="CARS_ID"))
public Cars userCars;

//set and get methods omitted

}

Table CARS with Cars Entity

@Entity
   @Column(name="CARS")
   public class Cars{

    private int dateId;

    @Id
    @GeneratedValue(strategy=IDENTITY)
    @Column(name="DATE_ID")
    public int getDateId() {
        return dateId;
    }

    public void setDateId(int dateId) {
        this.dateId = dateId;
    }


   @ManyToMany
   @Cascade(org.hibernate.annotations.CascadeType.ALL)
   @JoinTable(name="USER_CARS_JOINTABLE", 
               joinColumns=@JoinColumn(name="CARS_ID"),
               inverseJoinColumns=@JoinColumn(name="USER_ID"))

   public User username;

   //set and get methods omitted

   }

As you can see I have a JoinTable "USER_CARS_JOINTABLE" between USER and CARS which contain FK on User and Cars tables.

I also will have the table DESCRIPTION and Entity class the name Description:

@Entity
  @Column(name="DESCRIPTION")
  public class Description{

  private int id;

    @Id
    @GeneratedValue(strategy=IDENTITY)
    @Column(name="ID")
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }


  private String color;
  private String size;
  private String speed;
  private String type;

  //set and get methods omitted

  }

My JoinTable USER_CARS_JOINTABLE is:

USER_ID INT NOT NULL, CARS_ID INT NOT NULL, DESCRIPTION_ID INT NOT NULL, PRIMARY KEY(INSTITUTION_ID, DATE_ID, SRC_ID), CONSTRAINT FK_USER_ID_IN_USER_CARS FOREIGN KEY(USER_ID) REFERENCES USER(ID), CONSTRAINT FK_CARS_ID_IN_USER_CARS FOREIGN KEY(CARS_ID) REFERENCES CARS(ID), CONSTRAINT FK_DESCRIPTION_ID_IN_USER_CARS FOREIGN KEY(DESCRIPTION_ID) REFERENCES DESCRIPTION(ID)

In my case User can have alot's of Cars and one type of Car can belong to number of Users. Also the same Car can have different Description. Between class Description and User Entity and Cars Entity I will have an Undirectional, JoinTable, OneToMany relationships.That's mean that Classes User and Cars will know about Description class, but Description will not know about User and Cars. When I will try to enset data to my database from User side to class Cars and class Description I will have an exception like this:

Hibernate: insert into USER_CARS_JOINTABLE (User_ID, Description) values (?, ?) ERROR: org.hibernate.util.JDBCExceptionReporter - Field 'Cars_ID' doesn't have a default value ERROR: org.hibernate.event.def.AbstractFlushingEventListe ner - Could not synchronize database state with session

If I will do this from Cars side I will have an error like this:

Hibernate: insert into USER_CARS_JOINTABLE (Cars_ID, Description) values (?, ?) ERROR: org.hibernate.util.JDBCExceptionReporter - Field 'User_ID' doesn't have a default value ERROR: org.hibernate.event.def.AbstractFlushingEventListe ner - Could not synchronize database state with session

That's meant that some of my foreign keys an JoinTable USER_CARS_JOINTABLE did not get an ID because I'm inserting it from side which is can not to generate or insert data to this jointable USER_CARS_JOINTABLE, I don't know why?

If I will add to my JoinTable database schema for some of my foreign key like User_ID or Cars_ID the DEFAULT "number" than it is works, it's inser data, but it will always insert the same Number by DEFAULT which I put in "number". But What I want, is to give for one of my problem foreign key's in joinTable USER_CARS_JOINTABLE the same value as in parient table User or Cars!!! Maybe I can use some of the @Generator annotation from Hibernate for my JoinTable in my Entity class.

Thank you.

Your mapping is wrong. A bidirectional association always has an owner side and an inverse side. You made both sides the owner side. The inverse side must not contain mapping information (join table, join column), and must be marked as the inverse side using the mappedBy attribute:

@ManyToMany(cascade = CascadeType.ALL, mappedBy = "userCars")
private User username;

Note that you have several other problems with the code:

  • the naming is bad. Cars should be named Car. username should be named user. userCars should be named cars.
  • the username field shouldn't be public but private
  • the annotations should always be on the fields, or always be on the getters. You can't mix both placement types. If you do, half of them will be ignored by Hibernate

First, you are using ManyToMany over a single field. It's better to have something like this:

In your User Class:

@ManyToMany
@Cascade(org.hibernate.annotations.CascadeType.ALL)
@JoinTable(name="USER_CARS_JOINTABLE", 
            joinColumns=@JoinColumn(name="USER_ID"),
            inverseJoinColumns=@JoinColumn(name="CARS_ID"))
public List<Cars> userCars;

And in your Car class it is better to user mappedBy

@ManyToMany(mappedBy = "userCars")
public List<User> usernames;

In this case, you are making this relation bidirectional, and avoiding an unnecessary table (for this ManyToMany relation, one table is sufficient)

I believe your problem is because you are using the strategy IDENTITY to generate your IDs. Try setting this to AUTO instead.

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="ID")

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