Hibernate注释级联不起作用

发布于 2024-08-27 03:53:28 字数 3255 浏览 2 评论 0原文

我决定使用 hibernate 将 hbm.xml 样式更改为注释。 我在 hbm.xml 中有:

<hibernate-mapping package="by.sokol.jpr.data">
 <class name="Licence">
  <id name="licenceId">
   <generator class="native" />
  </id>
 <many-to-one name="user" lazy="false" cascade="save-update" column="usr"/>
 </class>
</hibernate-mapping>

并将其更改为:

@Entity
public class Licence {

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 private int licenceId;

 @ManyToOne(targetEntity=User.class, fetch=FetchType.EAGER, cascade = CascadeType.ALL)
 @Cascade(value = { org.hibernate.annotations.CascadeType.SAVE_UPDATE })
 private User user;
}

用户类:

@Entity(name = "Usr")
public class User {

    // BEGIN user info
    @Basic
    private String uid;
    @Basic
    private String name;
    @Basic
    private String company;
    @Basic
    private String street;

    // user's zip code
    @Basic
    private String ubication;
    @Basic
    private String city;
    @Basic
    private String po;

    @Column(name = "useremail")
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "assignedGen")
    @GenericGenerator(name = "assignedGen", strategy = "assigned")
    private String email;
    @Basic
    private String challengPassword;
    @Basic
    private String serialNumber;
}

Hibernate.cfg.xml

...
<mapping class="by.sokol.jpr.data.Licence" />
<mapping class="by.sokol.jpr.data.User" />
...

用于获取会话的

...
AnnotationConfiguration cfg = new AnnotationConfiguration();
cfg.configure(new File(PROPERTIES_FILENAME));
sessionFactory = cfg.buildSessionFactory();
...

Java 代码用于保存许可证对象

org.hibernate.Transaction t = session.beginTransaction();
session.saveOrUpdate(licence);
t.commit();

生成的 sql:

Hibernate: select this_.licenceId as licenceId0_2_, this_.creationDate as creation2_0_2_, this_.limitDate as limitDate0_2_, this_.user_useremail as user4_0_2_, this_.workstation_motherboardId as workstat5_0_2_, user1_.useremail as useremail1_0_, user1_.challengPassword as challeng2_1_0_, user1_.city as city1_0_, user1_.company as company1_0_, user1_.name as name1_0_, user1_.po as po1_0_, user1_.serialNumber as serialNu7_1_0_, user1_.street as street1_0_, user1_.ubication as ubication1_0_, user1_.uid as uid1_0_, workstatio4_.motherboardId as motherbo1_2_1_, workstatio4_.computerName as computer2_2_1_, workstatio4_.macAddress as macAddress2_1_, workstatio4_.osId as osId2_1_ from Licence this_ inner join Usr user1_ on this_.user_useremail=user1_.useremail left outer join Workstation workstatio4_ on this_.workstation_motherboardId=workstatio4_.motherboardId where user1_.useremail=?
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into Licence (creationDate, limitDate, user_useremail, workstation_motherboardId, licenceId) values (?, ?, ?, ?, ?)

APPEND_1:工作代码

org.hibernate.Transaction t = session.beginTransaction();
session.save(licence.getUser());
session.save(licence);
t.commit();

并且 hibernate 不会在保存时保存用户。我真的需要帮助!

I've decided to change hbm.xml style to annotations using hibernate.
I had in my hbm.xml:

<hibernate-mapping package="by.sokol.jpr.data">
 <class name="Licence">
  <id name="licenceId">
   <generator class="native" />
  </id>
 <many-to-one name="user" lazy="false" cascade="save-update" column="usr"/>
 </class>
</hibernate-mapping>

And changed it to:

@Entity
public class Licence {

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 private int licenceId;

 @ManyToOne(targetEntity=User.class, fetch=FetchType.EAGER, cascade = CascadeType.ALL)
 @Cascade(value = { org.hibernate.annotations.CascadeType.SAVE_UPDATE })
 private User user;
}

User class:

@Entity(name = "Usr")
public class User {

    // BEGIN user info
    @Basic
    private String uid;
    @Basic
    private String name;
    @Basic
    private String company;
    @Basic
    private String street;

    // user's zip code
    @Basic
    private String ubication;
    @Basic
    private String city;
    @Basic
    private String po;

    @Column(name = "useremail")
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "assignedGen")
    @GenericGenerator(name = "assignedGen", strategy = "assigned")
    private String email;
    @Basic
    private String challengPassword;
    @Basic
    private String serialNumber;
}

Hibernate.cfg.xml

...
<mapping class="by.sokol.jpr.data.Licence" />
<mapping class="by.sokol.jpr.data.User" />
...

Java code to get session

...
AnnotationConfiguration cfg = new AnnotationConfiguration();
cfg.configure(new File(PROPERTIES_FILENAME));
sessionFactory = cfg.buildSessionFactory();
...

Java code for saving Licence object

org.hibernate.Transaction t = session.beginTransaction();
session.saveOrUpdate(licence);
t.commit();

generated sql:

Hibernate: select this_.licenceId as licenceId0_2_, this_.creationDate as creation2_0_2_, this_.limitDate as limitDate0_2_, this_.user_useremail as user4_0_2_, this_.workstation_motherboardId as workstat5_0_2_, user1_.useremail as useremail1_0_, user1_.challengPassword as challeng2_1_0_, user1_.city as city1_0_, user1_.company as company1_0_, user1_.name as name1_0_, user1_.po as po1_0_, user1_.serialNumber as serialNu7_1_0_, user1_.street as street1_0_, user1_.ubication as ubication1_0_, user1_.uid as uid1_0_, workstatio4_.motherboardId as motherbo1_2_1_, workstatio4_.computerName as computer2_2_1_, workstatio4_.macAddress as macAddress2_1_, workstatio4_.osId as osId2_1_ from Licence this_ inner join Usr user1_ on this_.user_useremail=user1_.useremail left outer join Workstation workstatio4_ on this_.workstation_motherboardId=workstatio4_.motherboardId where user1_.useremail=?
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into Licence (creationDate, limitDate, user_useremail, workstation_motherboardId, licenceId) values (?, ?, ?, ?, ?)

APPEND_1: working code

org.hibernate.Transaction t = session.beginTransaction();
session.save(licence.getUser());
session.save(licence);
t.commit();

And hibernate doesn't save user on saving. I really need help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筱果果 2024-09-03 03:53:28

我建议删除 @Cascade 注释,除非您需要 delete-orphan

仅使用 cascade = {CascadeType.MERGE, CascadeType.PERSIST}

另一件需要观察的事情是您是否正在使用 AnnotationConfiguration。如果没有,则根本不会解析您的注释。

更新:您确定您的用户已设置电子邮件字段吗?我建议为 User 的主键设置一个自动生成的 id。 email是业务密钥,需要在其上实现hashCode()equals()

I'd suggest getting rid of the @Cascade annotation, unless you need delete-orphan.

use only cascade = {CascadeType.MERGE, CascadeType.PERSIST}

Another thing to observe is whether you are using AnnotationConfiguration. If not, your annotations aren't parsed at all.

Update: Are you sure your user have the email field set? I'd suggest having an auto-generated id for the primary key of User. The email is the business key, on which hashCode() and equals() should be implemented.

堇色安年 2024-09-03 03:53:28

您的用户是否定义为实体?不确定它是否可以与 hbm 和注释混合使用。
关系是双向的吗?也许你可以显示用户类别?

Is your user defined as an entity? Not sure if it works with hbm and annotations mixed.
Is the relation bidirectional? Maybe you can show user class?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文