4

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

セッションを取得する Java コード

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

License オブジェクトを保存するための 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();

また、休止状態は保存時にユーザーを保存しません。本当に助けが必要です!

4

2 に答える 2

3

必要でない限り、@Cascade注釈を削除することをお勧めしますdelete-orphan

のみ使用cascade = {CascadeType.MERGE, CascadeType.PERSIST}

観察すべきもう 1 つのことは、 を使用しているかどうかですAnnotationConfiguration。そうでない場合、注釈はまったく解析されません。

更新:ユーザーにemailフィールドが設定されていますか? の主キーに対して自動生成された ID を使用することをお勧めしますUser。は、とを実装する必要がemailあるビジネス キーです。hashCode()equals()

于 2010-03-29T14:56:33.600 に答える
0

ユーザーはエンティティとして定義されていますか? hbm と注釈が混在した状態で動作するかどうかは不明です。関係は双方向ですか?ユーザークラスを表示できますか?

于 2010-03-29T16:31:06.247 に答える