Reputation: 657
currently, I have an import.sql with which I import some test data in my database. Now, I want to bring it to our production system and what I read so far is that I should not use the import.sql in production.
Therefore, I thought I can create something with @Postconstruct.
I, therefore, created in the main application class something like that:
@Autowired
ICreateUserAtStartup repo;
@PostConstruct
public void initIt() throws Exception {
Rolle r = new Rolle();
r.setBezeichnung("xxx");
r.setId(1L);
r.setCreatedAt(new Date(2019, 01, 14));
repo.insertRolle(1L, "xxx");
}
In an seperate file I created the following interface:
@Repository
public interface ICreateUserAtStartup {
@Modifying
@Query("insert into benutzer(id, created_at, anzeigename,
benutzername, dienstnummer, active, passwort) SELECT :id,
:created_At, :anzeigename, :benutzername, :dienstnummer, :active,
:passwort")
void insertBenutzer(@Param("id") Long id, @Param("created_at")
String created_at, @Param("anzeigename") String anzeigename,
String benutzername, String dienstnummer, Boolean active, String password);
@Modifying
@Query("insert into rolle(id, bezeichnung) SELECT (:id,
:bezeichnung)")
void insertRolle(@Param("id") Long id, @Param("bezeichnung")
String bezeichnung);
}
However, as soon as I try to autowire repo in my main class, I always get the following exception:
No qualifying bean of type 'x.y.z.repository.ICreateUserAtStartup' available
Upvotes: 0
Views: 1611
Reputation: 3416
Why don't you just use a specific migration tool for this purpose like Flyway or Liquibase?
The reason why it cannot be autowired is that you haven't implemented that interface and created a bean from the implementation. Of course you might think that if you just create an interface and annotate it with @Repository
, it will work out of the box but that's not the case.
If you want to use Spring Data for your repository layer, you'll need an entity and you need to extend at least CrudRepository
.
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories
Upvotes: 1