【问题标题】:Hibernate annotations cascading doesn't work休眠注释级联不起作用
【发布时间】:2011-02-02 02:05:59
【问题描述】:

我决定将 hbm.xml 样式更改为使用 hibernate 的注释。 我的 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;
}

休眠.cfg.xml

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

获取会话的Java代码

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

并且休眠不会节省用户的保存时间。我真的需要帮助!

【问题讨论】:

  • 我只想指出应该拼写为“License”
  • 如何保存实体?生成什么sql?
  • @Kevin:“Licence”是正常的英国拼写。例如:tvlicensing.co.uk 美式版本正在逐渐普及,就像许多其他美式拼写一样,但英式版本完全正确。
  • @T.J.我不知道。感谢您的信息!
  • 请回答Rpond的问题

标签: java hibernate annotations cascade


【解决方案1】:

我建议去掉@Cascade 注释,除非你需要delete-orphan

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

要注意的另一件事是您是否使用AnnotationConfiguration。如果没有,则根本不会解析您的注释。

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

【讨论】:

  • 这是我在 stackoverflow 上的第一次体验,我喜欢它的快速解答。以下是我可以根据回复添加的内容: 1) cascade = {CascadeType.MERGE, CascadeType.PERSIST} - 我尝试过但没有成功
  • 将这些添加到您的问题中,而不是作为 cmets
  • 抱歉,这是我的第一次体验。我已在我的问题中添加了信息
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多