2012-04-25 4 views
1

を返す私のエンティティの抜粋である(それはまた、ハッシュコードを持っており、作成した等しくjavaのHibernateはいつもここ

ここ
@Entity 
@Table(name = "media_tspec_external_registry") 
public class Registry implements Serializable { 


public Registry() { 
    //for hibernate :D 
} 



public Registry(String show, String protocol, String externalEndpoint, String userURI, String version) { 
    super(); 
    this.show = show; 
    this.protocol = protocol; 
    this.externalEndpoint = externalEndpoint; 
    this.userURI = userURI; 
    this.version = version; 
} 




@Id 
@Column(name = "show", nullable = false) 
private String show; 

@Id 
@Column(name = "protocol", nullable = false) 
private String protocol; 

@Column(name = "external_endpoint", nullable = true) 
private String externalEndpoint; 

によって生成されるデフォルトのものであるにしようとしている私の方法であり、これらのキーの値

Registry reg = new Registry("invalid", "idvalue", null, null, null); 
    Registry reg2 = null; 
    try { 

     reg2 = (Registry) session.load(Registry.class, reg); 
      } catch (HibernateException e) { 
     throw new UserException("A registry entry does not exist for this show: " + show + " and protocol: " + protocol); 
     } 

に基づいて、存在しないエンティティをロードし、それは例外とREG2は今nullに設定されているすべてのフィールドを持つレジストリオブジェクトに設定されているスローすることはありません。

私はまた、負荷が既存のエンティティを読み込まないことにも気付きました。私は(非既存のオブジェクトにnullを返す有効なオブジェクトをロードする)期待どおりに動作する代わりに取得

reg2 = (Registry) session.get(Registry.class, reg); 

を使用する場合

は、しかし、任意の説明をいただければ幸いです。

ありがとう

+0

奇妙な。レジストリは同時にエンティティと複合キーの両方ですか? –

+0

@PiotrKochański[hibnate docs](http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-mapping-identifier)セクション2.2.3.2.2に基づいています。私は、エンティティ自身がシリアライズ可能な識別子(idclassまたは組み込みIDに似ている)であることを意味するようにしました –

+0

質問への回答をご覧くださいhttp://stackoverflow.com/q/608947/971040 それはあなたを右に向けるかもしれません方法。 – viktor

答えて

1

これは予想される動作です。 session.loadは、参照を満たすために使用できるオブジェクトを取得するためのものです。

User u = new User(); 
u.setRole((Role)session.load(Role.class, 5)); 
session.save(u); 

ロードは往復を生成しません。使用可能なオブジェクトへの参照がない場合は、proxyオブジェクトを作成します。そうでない場合は、コンポジットキーオブジェクトをリサイクルし、エンティティが存在するかどうかをデータベースに問い合わせることはできません。

関連する問題