2017-06-16 4 views
1

私は、自動的に 'LASTMODIFIED' フィールドを更新するためにorg.hibernate.annotations.Generated @使用したい動作しません、ここに私のコードは次のとおりです。休止@Generatedアノテーションがうまく

ビル:

@Entity 
public class Bill { 
    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE) 
    protected Long id; 

    @Temporal(TemporalType.TIMESTAMP) 
    @Column(insertable = false, updatable = false) 
    @org.hibernate.annotations.Generated(GenerationTime.ALWAYS) 
    protected Date lastModified; 

    @Column 
    protected String description; 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    public Date getLastModified() { 
     return lastModified; 
    } 

    public void setLastModified(Date lastModified) { 
     this.lastModified = lastModified; 
    } 

    public String getDescription() { 
     return description; 
    } 
} 

メイン:私は手動でSQLで手形を確認

public static void main(String[] args) { 
     Session session = factory.openSession(); 
     Transaction tx = session.beginTransaction(); 
     Bill bill1 = new Bill(); 
     session.persist(bill1); 
     Bill bill2 = new Bill(); 
     session.persist(bill2); 
     bill1.setDescription("Bill1 modified"); 
     tx.commit(); 
     factory.close(); 
    } 

、それは示した:

ID

|説明| lastModified |

--- | -------------- | ---------------- |

1 |ビル1修正|ヌル
2 | null | null

私の休止状態のバージョンは5.2.9です。ファイナル、mysqlのバージョンは5.7.16です。私は 'insertable = false、updatable = false'を削除しようとしましたが、どちらもうまくいきませんでした。

+0

可能な重複[JPA注釈付きMYSQLの自動インクリメントフィールドに注釈を付ける方法](https://stackoverflow.com/questions/4102449/how-to-annotate-mysql-:

詳細については、以下のリンクを参照してください。オートインクリメントフィールド付きjpaアノテーション) –

答えて

1

代わり@CreationTimestampと@UpdateTimestampを使用することができます。論文の注釈で生成

@CreationTimestamp 
private Date created; 
@UpdateTimestamp 
private Date lastModified; 

タイムスタンプは、VM時間に基づいて、メモリ内の世代です。

データベースで生成されたタイムスタンプが必要な場合は、@ ValueGenerationTypeでマークされたカスタムアノテーションを使用する必要があります。 http://docs.jboss.org/hibernate/orm/4.3/topical/html/generated/GeneratedValues.html#_in_database_generation