4

I am new to hibernate, yet not to Java or databases. I try to establish a @OneToMany Relationship, but somehow I can get it to work. I tried for two days now and I googled a lot, yet I still get Exceptions.
For getting to know hibernate I try to create a little server which is sending messages forth and back to a client. This works well - also the reading from the database with hibernate works fine. Now my idea was, that when a user enters the system, a sessionid is created and stored with the timestamp in the database ... and there the problem starts.
I will post my code, so you will understand.

@Entity
@NamedQuery(name="CustomerLogin.getPassByEmail", query="from CustomerLogin where email = ?")
@Table(name="customer_login")
public class CustomerLogin {

@Id @GeneratedValue(strategy = GenerationType.AUTO)
private BigInteger id;
private String email;
private String pass;
@OneToMany
//@JoinTable(name="customerlogin_sessions", joinColumns=@JoinColumn(name="user_id"))
private Collection<CustomerSession> sessions = new ArrayList<>();

I will not post the getters and setters. I think you'll appreciate it ;) You see I removed the @JoinTable - because it doesn't work. ... And now the CustomerSession class:

@Entity

@Table (name="customer_session") public class CustomerSession {

@Id @GeneratedValue(strategy= GenerationType.AUTO)
private BigInteger id;
@ManyToOne 
private CustomerLogin customerLogin;
private BigInteger userid;
private int sessionid;
@Temporal(TemporalType.TIMESTAMP)
private Date logintime;

And here the magic should happen:

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

    Query query = session.getNamedQuery("CustomerLogin.getPassByEmail");
    query.setString(0, commands.get("email"));
    List<CustomerLogin> passes = (List<CustomerLogin>)query.list();


    if(evaluateLoginData(passes)){ //consider it true
        CustomerLogin customer = passes.get(0);
        int sessionId = createSessionId();

        CustomerSession customerSession = new CustomerSession();
        customerSession.setLogintime(new Date());
        customerSession.setCustomerLogin(customer);
        customerSession.setSessionid(sessionId);

        customer.getSessions().add(customerSession);

        session.save(customerSession);
        session.save(customer);


        returnMessage =  "LOGINACC id:"+customer.getId() + ";session:"+Integer.toString(sessionId);
    }else{
        returnMessage = "LOGINDENIED message:LOGINDENIED";
    }

    session.getTransaction().commit();
    session.close();

I will also post the hibernate.cfg.xml - maybe there is a problem.

<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property>
<property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property>
<property name="hibernate.connection.url"> jdbc:mysql://localhost/server </property>

<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>

<property name="show_sql">true</property>
<property name="hibernate.connection.pool_size">5 </property>
<property name="hdm2ddl.auto">update</property>

<mapping class="balancer.dbpojos.CustomerLogin"/> 
<mapping class="balancer.dbpojos.CustomerSession"/> 

As far as I understand, when I just use @OneToMany and @ManyToOne hibernate is supposed to make a new table with the relationship. Yet it says, table customerlogin_customersession not found - even if I change the hdm2ddl.auto to create. I tried the myppedBy attribute and targetEntity, same with the @JoinTableJ,JoinColumns and @InverseJoinColumns - still no effect.

I just want it to work - no matter how the tables look like in the end. But if there is a hibernate guru out there - I would love to have a foreign key in the customerSssion, pointing to the customerLogin as a foreign key relationship. But, like I said, I am fiddeling around that problem for almost 2 days now - if it would only work, I would be sooo happy. Thanks in advance for your effort and your time.

Daniel
  • 72
  • 1
  • 2
  • 8

1 Answers1

4

Note: I am far from an expert, it just seems your mapping is somewhat backwards to me.

You shouldn't have the @JoinColumn on the @OneToMany side at all.

Something like this should be closer:

@OneToMany(mappedBy="id")
private Collection<CustomerSession> sessions = new ArrayList<>();

@JoinColumn(name = "ID", referencedColumnName = "ID")
@ManyToOne 
private CustomerLogin customerLogin;

Further reading: Can someone please explain mappedBy in hibernate?

Community
  • 1
  • 1
MasNotsram
  • 2,105
  • 18
  • 28
  • It works - thanks a lot. I would like to give you some points, but I am so new, I don't have any reputation - I am so sorry. I read your link and I know now that I viewed the whole thing from the wrong perspective. Thank you so much. – Daniel Apr 11 '13 at 20:59
  • @Daniel Sorry, must have missed this comment. Don't worry about the rep, accepting the answer is plenty thanks. Glad I could help, best of luck. – MasNotsram May 13 '13 at 12:38