Spring Bootマジック、
Spring Data JPA 、およびエンティティ監査の作業の例。
設定全体はJava Configを使用してクラスで記述されますが、
application.propertiesには
application.propertiesファイルがあり
application.properties 。 これは、Spring Bootが初期化の非常に早い段階でこれらの設定を選択し、いくつかのデフォルト設定を置き換える必要があるために使用されます。
H2 Database Engineをデータベースとして使用します。
デフォルトでは、HSQL、H2、またはDerbyデータベースドライバーを接続するときのSpring Data JPAのSpringブートは、メモリ内データベースでDataSourceを作成し、アプリケーションリソースの
schema.sqlおよび
data.sql初期化し
data.sql 。 また、デフォルトで
hibernate.hbm2ddl.auto=create-drop使用されます。その後、エンティティから生成されたテーブルを備えた初期データベースが取得されます。 これが行われる理由は謎ですが、この自動
spring.jpa.hibernate.ddl-auto=noneは
application.propertiesファイルのパラメーターによって無効にする必要があります
spring.jpa.hibernate.ddl-auto=noneまた、DataSourceに加えて、Spring BootはEntityManagerFactoryを親切に作成し、アプリケーション内の任意の場所でエンティティを検索します。
アプリケーションをさらにカスタマイズするには、AppConfigクラスを作成します。
@Configuration @EnableTransactionManagement @EnableJpaAuditing public class AppConfig { @Bean public AuditorAware<User> auditorProvider() { return new AuditorAwareImpl(); } }
@Configurationこのファイルにアプリケーションを構成するためのBeanが含まれていることをSpring Bootに伝えます。@EnableTransactionManagementトランザクションサポートを有効にし、デフォルト設定で必要なBeanを作成します。@EnableJpaAuditing監査サポートが含まれますが、このサポートが機能するようにBeanを作成して、ユーザーにどこから@EnableJpaAuditingするかを説明する必要があります。
AuditorAwareImplクラス
AuditorAwareImplます:
public class AuditorAwareImpl implements AuditorAware<User> { @Autowired private CurrentUserService currentUserService; @Override public User getCurrentAuditor() { return currentUserService.getCurrentUser(); } }
CurrentUserServiceは、Userオブジェクトを返すサービスです。後で作成します。
ここで、エンティティクラスを作成する必要があります。ユーザーから始めます。
@Entity @EntityListeners({AuditingEntityListener.class}) public class User extends AbstractAuditable<User, Long> { @Basic @Column private String name; public String getName() { return name; } public void setName(String data) { this.name = data; } @Version @Column private Long version; public Long getVersion() { return version; } public void setVersion(Long version) { this.version = version; } @Override public String toString() { return "User {" + "id='" + getId() + "', " + "name='" + getName() + "'} "; } }
Spring Dataの抽象クラス
AbstractAuditable<U, PK>から継承します。ここで、
Uはユーザーに責任のある型で、
PKはメインキーの型です。 この継承の結果、本質的に、次のプロパティはすでに
id 、
createdBy 、
createdDate 、
lastModifiedByおよび
lastModifiedDateます。
便宜上、
nameプロパティを追加し、バージョン番号には、Spring Dataが管理するバージョンプロパティを追加します。 実際、Spring Dataは
nameを除くすべてのフィールドを管理します。
@Entity -JPAにこれがエンティティクラスであることを@Entity@EntityListeners({AuditingEntityListener.class}) -Spring Dataからエンティティのデフォルトのリスナークラスを追加します。 この行により、同じ設定のorm.xmlファイルが不要になります。
テーブルのプロパティとフィールドの対応、およびテーブルの名前、Springは、それらが一致する場合、もちろん、セパレータの有無のみが下線として許可されます。
2番目のクラス
Fooには
nameず、
dataプロパティがあり
data 。
次に、各エンティティのリポジトリを作成します。
public interface UserRepository extends CrudRepository<User, Long> { }
実際には、抽象クラス
CrudRepository<T, ID>おかげで、これはすべてリポジトリの作成です。ここで、
Tはエンティティのタイプ、
IDはメインキーのタイプです。 リポジトリ実装の残りは、Spring Dataに引き継がれます。
次に、
CurrentUserServiceサービスを作成します。
CurrentUserServiceサービスは、2人の異なるユーザーによる監査作業を実証するためにのみ必要で、注文と美しさのために作成されました。
@Service public class CurrentUserService { private Long currentUserID = 1L; @Autowired private UserRepository userRepository; public User getCurrentUser() { return userRepository.findOne(currentUserID); } public void setCurrentUserToJohn() { currentUserID = 1L; } public void setCurrentUserToDoe() { currentUserID = 2L; } }
@Serviceこのクラスが内部サービスを実装することをSpringに伝えます。@Autowired依存性注入を使用すると、ユーザーリポジトリのインスタンスが作成されます。
そして今、実際には、アプリケーションクラス:
@ComponentScan @EnableAutoConfiguration public class App implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(App.class, args); } @Autowired private FooRepository fooRepository; @Autowired private CurrentUserService currentUserService; @Override public void run(String... args) { Foo o = new Foo(); o.setData("test data"); fooRepository.save(o); fooRepository.findAll().forEach(System.out::println); currentUserService.setCurrentUserToDoe(); o.setData("New test data"); fooRepository.save(o); fooRepository.findAll().forEach(System.out::println); } }
春の魔法の作品。
アプリケーションソース:
Spring Data JPA Audit and Version ExamplePS
ニュアンス:
*
Joda-Time依存関係がプロジェクトに追加されました。これがない場合、タイムスタンプ付きのマジックは機能せず、createdDateおよびlastModifiedDateフィールドとそれらのタイプを手動で指定する必要があります。
*ユーザーは次のフィールドで追加されます。
insert into USER (ID, NAME, VERSION) values (1, 'John', 0);
バージョンがNULLの場合、Spring Dataのワイルドでエラーが発生します。USERが同じソースの作成者または修飾子として指定されている場合、Spring Data内の無限循環参照が原因でエラーが発生します。