【问题标题】:Can't get @OneToMany to work with hibernate无法让@OneToMany 与休眠一起使用
【发布时间】:2013-04-03 10:35:05
【问题描述】:

我是 hibernate 的新手,但对 Java 或数据库不熟悉。我尝试建立@OneToMany 关系,但不知何故我可以让它发挥作用。我现在尝试了两天,我用谷歌搜索了很多,但我仍然得到例外。
为了了解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<>();

我不会发布 getter 和 setter。我想你会很感激的;)你看我删除了@JoinTable——因为它不起作用。 ...现在是 CustomerSession 类:

@Entity

@Table (name="customer_session") 公共类 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;

魔法应该在这里发生:

        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();

我也会发布 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"/> 

据我了解,当我只使用 @OneToMany 和 @ManyToOne 时,休眠应该使用该关系创建一个新表。然而它说,找不到表 customerlogin_customersession - 即使我将 hdm2ddl.auto 更改为创建。我尝试了 myppedBy 属性和 targetEntity,与 @JoinTableJ、JoinColumns 和 @InverseJoinColumns 相同 - 仍然没有效果。

我只是希望它能够正常工作 - 无论表格最终看起来如何。但是,如果那里有一个休眠大师 - 我希望在 customerSssion 中有一个外键,指向 customerLogin 作为外键关系。但是,就像我说的那样,我现在已经在这个问题上摆弄了将近 2 天——如果它真的有效,我会很高兴的。 提前感谢您的努力和时间。

【问题讨论】:

    标签: hibernate one-to-many many-to-one


    【解决方案1】:

    注意:我远非专家,只是您的映射似乎对我有些倒退。

    您根本不应该在@OneToMany 一侧使用@JoinColumn。

    这样的东西应该更接近:

    @OneToMany(mappedBy="id")
    private Collection<CustomerSession> sessions = new ArrayList<>();
    
    @JoinColumn(name = "ID", referencedColumnName = "ID")
    @ManyToOne 
    private CustomerLogin customerLogin;
    

    进一步阅读: Can someone please explain mappedBy in hibernate?

    【讨论】:

    • 它有效 - 非常感谢。我想给你一些分数,但我太新了,我没有任何名声——我很抱歉。我阅读了您的链接,现在我知道我从错误的角度看待整个事情。非常感谢。
    • @Daniel 抱歉,一定错过了这条评论。不用担心代表,接受答案非常感谢。很高兴我能帮上忙,祝你好运。
    猜你喜欢
    • 2011-10-31
    • 2019-02-01
    • 1970-01-01
    • 2021-08-21
    • 2013-07-01
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    • 2017-07-30
    相关资源
    最近更新 更多