Mapowanie ManyToMany

0

Witam , chce przeprowadzić takie mapowanie klasy User z PCStation

package com.hibernate.user;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
@Table(name = "Stationes")
public class PCStation
{
    private int id;
    private String desc;
    private Set<User> users;

    public PCStation(){}

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "station_id",unique = true,nullable=false)
    public int getId() 
    {
        return id;
    }

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

    @Column(name = "opis",unique=false,nullable=true,length=100)
    public String getDesc()
    {
        return desc;
    }

    public void setDesc(String desc)
    {
        this.desc = desc;
    }

    @ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(name ="Users_Stationes",joinColumns = {@JoinColumn(referencedColumnName="station_id")}
                                        ,inverseJoinColumns={@JoinColumn(referencedColumnName="user_id")})  
    public Set<User> getUsers()
    {
        return users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }

}
package com.hibernate.user;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "user")
public class User 
{   
    private int id;
    private String name;
    private String surname;
    private UserInfo info;
    private Set<Phone> phones;
    private Set<PCStation> stationes;

    public User() {}

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id", unique = true, nullable = false)
    public int getId() 
    {
        return id;
    }

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

    @Column(name = "name", nullable = false)
    public String getName()
    {
        return name;
    }

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

    @Column(name = "surname", nullable = false)
    public String getSurname() 
    {
        return surname;
    }

    public void setSurname(String surname)
    {
        this.surname = surname;
    }

    @Override
    public String toString()
    {
        return "[ID: "+id+" NAME: "+name+" SURNAME: "+surname+"]";
    }

    @OneToMany(cascade=CascadeType.ALL,mappedBy = "user")   
    public Set<Phone> getPhones() 
    {
        return phones;
    }

    public void setPhones(Set<Phone> phones) 
    {
        this.phones = phones;
    }

    @OneToOne(cascade=CascadeType.ALL,mappedBy="user")  
    public UserInfo getInfo()
    {
        return info;
    }

    public void setInfo(UserInfo info) 
    {
        this.info = info;
    }

    @ManyToMany(mappedBy="users")
    public Set<PCStation> getStationes() 
    {
        return stationes;
    }

    public void setStationes(Set<PCStation> stationes)
    {
        this.stationes = stationes;
    }

}
public static void main(String[] args)
    {       

        Configuration configuration = new Configuration().configure(HibernateClient.class
                .getResource("hibernate.cfg.xml"));
        StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
                configuration.getProperties()).build();
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

        Session session = null;
        Transaction transaction = null;

        try
        {

            session = sessionFactory.openSession();
            transaction = session.beginTransaction();

            User user = new User();
            user.setName("Pete");
            user.setSurname("Piotrowski");

            User user2 = new User();
            user.setName("David");
            user.setSurname("Davidson");

            Set<User> users = new HashSet<User>();
            users.add(user);
            users.add(user2);

            PCStation s1 = new PCStation();
            s1.setDesc("Nr.1");

            PCStation s2 = new PCStation();
            s2.setDesc("Nr.2");

            s1.setUsers(users);
            s2.setUsers(users);         

            session.save(s1);
            session.save(s2);           

            transaction.commit();
        }
        catch(Exception ex)
        {
            transaction.rollback();
        }
        finally
        {
            sessionFactory.close();
        }

Hibernate generuje odpowienie tabele jednak nie zapisuje do nich danych. Co może być tego przyczyną?

0

@Update, Oki znalazłem już błąd. Zmęczenie, wieczór dają znać o sobie niestety.

@Update2
Otóż problem polegał tylko i wyłącznie na literówce, dodawałem do kolumny nullable=false wartość pusta poprzez literówkę w zmiennej;)

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0