0

so I'm trying to figure out the relationship of MySQL, I have the code written but it doesn't seem to be working, as if I add something to my database the foreighn key of the other table every time gives me NULL, the code I have for my relationships are:

User Class:

    @OneToMany(mappedBy="user")
private Collection<Movie> movies;

    public Collection<Movie> getMovies() {
    return movies;
}
public void setMovies(Collection<Movies> movies) {
    this.movies = movies;
}

Movies Class:

@ManyToOne
    private User user;

    public User getUser() {
    return user;
}
public void setUser(User user) {
    this.user = user;
}

Every time I add a new movies (which is suppose to be connected with the users table) the foreign key (user_id) gives me null in the movies table. To summarize what I'm trying to achieve here, every time I want to add a movie to my library, I want the user id of the person who has the movie to show in the database.

Thanks.

EDIT:

I keep getting this error message every time I try and run it:

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: entity.Movie.user
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:226)
at persistence.PersistenceUtil.persist(PersistenceUtil.java:26)
at main.CustomerConfig.main(CustomerConfig.java:47)

Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: entity.Movie.user
    at org.hibernate.engine.Nullability.checkNullability(Nullability.java:95)
    at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:313)
    at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
    at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:49)
    at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:154)
    at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:110)
    at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61)
    at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:645)
    at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:619)
    at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:623)
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:220)

Code that create and saves data to database:

System.out.println("Type the users's name please. ");
        String name = br.readLine();

        System.out.println("Type the movies name please. ");
        String mname = br.readLine();

        User user = new User(name);
        PersistenceUtil.persist(user);
        Movie movie = new Movie(mname);
        PersistenceUtil.persist(movie);
        System.out.println("User & movie names are created!");
hamod90
  • 111
  • 1
  • 2
  • 7

1 Answers1

0
Movie movie = new Movie(mname);
PersistenceUtil.persist(movie);

As the error says, user is null for this movie object, you've forgotten to set the user in it. This is how it's suppposed to look like :

Movie movie = new Movie(mname);
movie.setUser(user);
PersistenceUtil.persist(movie);
coding_idiot
  • 13,526
  • 10
  • 65
  • 116