0

fk id、ブール値、およびタイムスタンプを格納する JPA エンティティがあります。

@Entity
public class ChannelInUse implements Serializable {
  @Id
  @GeneratedValue
  private Long id;
  @ManyToOne
  @JoinColumn(nullable = false)
  private Channel channel;
  private boolean inUse = false;
  @Temporal(TemporalType.TIMESTAMP)
  private Date inUseAt = new Date();
  ...
 }

このエンティティのすべての新しいインスタンスがテーブルの新しい行になるようにします。何らかの理由で、私が何をしても、新しい行を作成するのではなく、常に行が新しいタイムスタンプ値で更新されます。ネイティブクエリを使用して挿入を実行しようとしましたが、チャネルの ID がまだ入力されていなかったので、あきらめました。channel.getId と inUseAt で構成される埋め込み ID クラスを使用してみました。私のイコールとハッシュコードは次のとおりです。

 public boolean equals(Object obj){
  if(this == obj)
   return true;
  if(!(obj instanceof ChannelInUse))
   return false;
  ChannelInUse ciu = (ChannelInUse) obj;
  return ( (this.inUseAt == null ? ciu.inUseAt == null : this.inUseAt.equals(ciu.inUseAt)) 
    && (this.inUse == ciu.inUse) 
    && (this.channel == null ? ciu.channel == null : this.channel.equals(ciu.channel))
    );
 }
 /**
  * hashcode generated from at, channel and inUse properties. 
  */
 public int hashCode(){
  int hash = 1;
  hash = hash * 31 + (this.inUseAt == null ? 0 : this.inUseAt.hashCode());
  hash = hash * 31 + (this.channel == null ? 0 : this.channel.hashCode());
  if(inUse)
   hash = hash * 31 + 1;
  else
   hash = hash * 31 + 0;
  return hash;
 }
}

mutable=false で休止状態の Entity アノテーションを使用してみました。私はおそらく、何がエンティティをユニークにするのか、または何かを理解していないだけです。グーグルをかなり強く叩きますが、これを理解することはできません。

更新: 永続化コードの追加:

public void store(Map<String, String> params,

        Map<?, ?> values) throws Exception {
    VoiceInterface iface = (VoiceInterface) getStorageUnit(params);
    ALeafPort leafPort = getLeafPort(iface);
    SortedSet<Channel> channels = leafPort.getChannels();
    Iterator<Channel> it = channels.iterator();
    while(it.hasNext()){
        Channel c = it.next();
        ChannelInUse ciu = new ChannelInUse(c,
                           ((Boolean) values.get(c.getNumber())).booleanValue());   
        em.persist(ciu);
    }
}

getStorageUnit と getLeafPort は、ストレージから適切なオブジェクトを検索します (存在しない場合は作成します)。

4

1 に答える 1

0

ええ、その hbm2ddl.auto=create プロパティに注意する必要があります。おっとっと!

于 2010-05-12T02:25:03.517 に答える