Can';我不能让@OneToMany与hibernate一起工作

Can';我不能让@OneToMany与hibernate一起工作,hibernate,one-to-many,many-to-one,Hibernate,One To Many,Many To One,我不熟悉hibernate,但不熟悉Java或数据库。我试图建立一种“一对一”的关系,但不知何故我能让它发挥作用。我已经试了两天了,我在谷歌上搜索了很多,但仍然有例外。 为了了解hibernate,我尝试创建一个小型服务器,向客户机来回发送消息。这工作得很好——使用hibernate从数据库读取数据也很好。现在我的想法是,当用户进入系统时,会创建一个sessionid,并将其与时间戳一起存储在数据库中。。。然后问题就开始了。 我会发布我的代码,这样你就会明白 @Entity @NamedQuer

我不熟悉hibernate,但不熟悉Java或数据库。我试图建立一种“一对一”的关系,但不知何故我能让它发挥作用。我已经试了两天了,我在谷歌上搜索了很多,但仍然有例外。
为了了解hibernate,我尝试创建一个小型服务器,向客户机来回发送消息。这工作得很好——使用hibernate从数据库读取数据也很好。现在我的想法是,当用户进入系统时,会创建一个sessionid,并将其与时间戳一起存储在数据库中。。。然后问题就开始了。
我会发布我的代码,这样你就会明白

@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<>();
@表(name=“客户会议”) 公共类客户会话{

@Id @GeneratedValue(strategy= GenerationType.AUTO)
private BigInteger id;
@ManyToOne 
private CustomerLogin customerLogin;
private BigInteger userid;
private int sessionid;
@Temporal(TemporalType.TIMESTAMP)
private Date logintime;
在这里,奇迹应该发生:

        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();
Session Session=sessionFactory.openSession();
session.beginTransaction();
Query Query=session.getNamedQuery(“CustomerLogin.getPassByEmail”);
query.setString(0,commands.get(“电子邮件”));
List passes=(List)query.List();
如果(evaluateLoginda(passes)){//则认为它为真
CustomerLogin customer=passes.get(0);
int sessionId=createSessionId();
CustomerSession CustomerSession=新CustomerSession();
customerSession.setLogintime(新日期());
customerSession.setCustomerLogin(客户);
customerSession.setSessionid(sessionId);
customer.getSessions().add(customerSession);
会话.保存(customerSession);
session.save(客户);
returnMessage=“loginac id:”+customer.getId()+“会话:”+Integer.toString(sessionId);
}否则{
returnMessage=“LOGINDENIED消息:LOGINDENIED”;
}
session.getTransaction().commit();
session.close();
我还将发布hibernate.cfg.xml-可能有问题

<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"/> 

org.hibernate.dialogue.mysqldialogue
com.mysql.jdbc.Driver
jdbc:mysql://localhost/server 
根
真的
5.
更新

据我所知,当我只使用@OneToMany和@ManyToOne时,hibernate应该用这种关系创建一个新表。但是它说,找不到表customerlogin_customersession-即使我更改hdm2ddl.auto来创建。我尝试了myppedBy属性和targetEntity,与@JoinTableJ、JointColumns和@InverseJoinColu相同mns-仍然没有效果

我只是希望它能够工作——不管表最终看起来是什么样子。但是如果有一个hibernate专家在那里——我希望在CustomerSession中有一个外键,将customerLogin作为外键关系指向它。但是,正如我所说的,我已经在这个问题上犹豫了将近两天——如果它能够工作,我会的一定要快乐。
提前感谢您的努力和时间。

注意:我远非专家,您的映射似乎有点落后于我

您根本不应该在@OneToMany一侧设置@join列

类似这样的东西应该更接近:

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

@JoinColumn(name = "ID", referencedColumnName = "ID")
@ManyToOne 
private CustomerLogin customerLogin;
@OneToMany(mappedBy=“id”)
私有收集会话=新建ArrayList();
@JoinColumn(name=“ID”,referencedColumnName=“ID”)
@许多酮
私人客户登录客户登录;
进一步阅读:

它很有效-非常感谢。我想给你一些分数,但我太新了,没有任何声誉-我很抱歉。我读了你的链接,现在知道我从错误的角度看待了整件事。非常感谢。@Daniel抱歉,一定错过了这条评论。不要担心代表,接受答案就足够了。谢谢.很高兴我能帮忙,祝你好运。
@OneToMany(mappedBy="id")
private Collection<CustomerSession> sessions = new ArrayList<>();

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