Answer the question
In order to leave comments, you need to log in
Why is the JPA repository not initializing?
Good afternoon!
I'm trying a bunch of kotlin - spring boot 2 - jpa I sketched a
simple program, but when I start it I get an error when initializing the JPA repository. What did you do wrong? Help fix the error?
I have not used Springboot before, I want to master it. I'm not going to use the web in this program, I reviewed many examples with the web, but I did not find the answer to my question.
2018-12-21 13:26:02.732 INFO 28188 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL95Dialect
2018-12-21 13:26:02.923 INFO 28188 --- [ main] o.h.e.j.e.i.LobCreatorBuilderImpl : HHH000421: Disabling contextual LOB creation as hibernate.jdbc.lob.non_contextual_creation is true
2018-12-21 13:26:02.929 INFO 28188 --- [ main] org.hibernate.type.BasicTypeRegistry : HHH000270: Type registration [java.util.UUID] overrides previous : [email protected]
2018-12-21 13:26:03.335 INFO 28188 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2018-12-21 13:26:04.150 INFO 28188 --- [ main] r.k.v.VkUsersSkillApplicationKt : Started VkUsersSkillApplicationKt in 4.348 seconds (JVM running for 4.905)
Exception in thread "main" kotlin.UninitializedPropertyAccessException: lateinit property vkUserRepository has not been initialized
at ru.program.vkUsersSkill.VkUsersSkillApplicationKt.main(VkUsersSkillApplication.kt:17)
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.Id
import javax.persistence.Table
@Entity
@Table (name = "vk_users")
data class VkUser(
@Id
@Column(name = "user_id")
var userId: Long = 0L,
@Column(name = "access_token")
var accessToken: String = "",
@Column(name = "alias")
var alias: String = "",
@Column(name = "login")
var login: String = "",
@Column(name = "password")
var password: String = ""
)
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
import ru.program.vkUsersSkill.models.VkUser
@Repository
interface VkUserRepository: JpaRepository<VkUser, Long>
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import ru.program.vkUsersSkill.repositories.VkUserRepository
@Autowired
lateinit var vkUserRepository: VkUserRepository
@SpringBootApplication
class VkUsersSkillApplication
fun main(args: Array<String>) {
runApplication<VkUsersSkillApplication>(*args)
vkUserRepository.findAll()
}
Answer the question
In order to leave comments, you need to log in
We create a start bin in which we will run everything.
@Component
class StartHere {
@Autowired
lateinit var vkUserRepository: VkUserRepository
fun runHere() {
var users = vkUserRepository.findAll()
println(users)
}
//TODO write the code we need here
}
Then get the 'application context' into a variable and then get our start bean and run its contents with the runHere function.
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import ru.program.vkUsersSkill.init.StartHere
@SpringBootApplication
class VkUsersSkillApplication
fun main(args: Array) {
val context = runApplication(*args)
val start = context.getBean(StartHere::class.java)
start.runHere()
}
Now everything starts without problems and using Spring-Jpa we get data from bases.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question