У меня есть kotlin проект весенней загрузки, этот проект - мой первый пример в kotlin, который я разработал. Я использую jpa и metamodel для построения запросов. когда я пытаюсь построить проект, я сталкиваюсь с сообщением об ошибке «не могу найти символ». Я приложил здесь исходный код. Может кто-нибудь построить мой исходный код и найти мою точную точку отказа. Я также добавил сущность Employee, классы репозитория и контекст файла pom.
@Entity
@Table(name = "tblEmployees")
open class Employee : Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "employeeId", unique = false, nullable = false)
var employeeId: Long? = null ;
@Column(name = "EmployeeGuid", unique = true, nullable = false, length = 20)
var employeeGuid: String? = null
@Column(name = "FirstName", unique = false, nullable = false, length = 20)
var firstName: String? = null
@Column(name = "LastName", unique = false, nullable = false, length = 20)
var lastName: String? = null
@Column(name = "Role", unique = false, nullable = false, length = 20)
var role: String? = null
@Column(name = "RegisterServerIP", unique = false, nullable = false, length = 20)
var registerServerIP: String? = null
@Column(name = "RegisterClientIP", unique = false, nullable = false, length = 20)
var registerClientIP: String? = null
@Column(name = "RegisterDate", nullable = false)
var registerDate: Int? = null
@Column(name = "RegisterTime", nullable = false, length = 8)
var registerTime: String? = null
@Column(name = "RegisterDateComplete", nullable = false, length = 50)
var registerDateComplete: String? = null
}
@Repository
interface RepoEmployee : JpaRepository<Employee, Long>, RepoEmployeeExtension {
fun findByFirstName(firstName: String): List<Employee> // default implementation
fun findByLastName(lastName: String): List<Employee> // default implementation
@Query("select e from Employee e where e.firstName like %:firstName%") // default implementation
fun findContains(@Param("firstName") firstName: String): List<Employee>
}
@Repository
interface RepoEmployeeExtension {
fun findInRange(from: Int, to: Int): List<Employee>
fun findEmployeeByIdOnNQ(id: Long): Employee?
fun searchEmployees(startIndex: Int, endIndex: Int ,employeeId: Long? , firstName: String? , lastName: String? , role: String? , registerDate : Int? , registerClientIP : String? ,
registerUserId: Long? , registerUserName: String? ): List<Employee>
}
class RepoEmployeeExtensionImpl : RepoEmployeeExtension {
@Autowired
private lateinit var entityManager: EntityManager
override fun findInRange(from: Int, to: Int): List<Employee> {
val builder = entityManager.getCriteriaBuilder()
val query = builder.createQuery(Employee::class.java)
val root = query.from(Employee::class.java)
query.select(root)
val q = entityManager.createQuery(query)
q.firstResult = from
q.maxResults = to
return q.resultList
}
override fun findEmployeeByIdOnNQ(id: Long): Employee? {
var employee = entityManager.createNamedQuery("NQfindEmployeeByCId").setParameter("id", id).resultList.stream().findFirst()
return employee as? Employee; // nullable cast
}
override fun searchEmployees(startIndex: Int, endIndex: Int , employeeId: Long?, firstName: String?, lastName: String?, role: String?, registerDate: Int?, registerClientIP: String?,
registerUserId: Long?, registerUserName: String?): List<Employee> {
val criteriaBuilder = entityManager.getCriteriaBuilder()
val query = criteriaBuilder.createQuery<Employee>(Employee::class.java)
val rootEmployee: Root<Employee> = query.from(Employee::class.java)
//Convert LazyLoad to eager in criteriaBuilder
// val rootArUserJoin= rootEmployee.join(Employee_.current_registerUser , JoinType.INNER)
// val rootArUserFetch= rootEmployee.fetch(Employee_.current_registerUser , JoinType.INNER)
val criterias = ArrayList<Predicate>()
if (employeeId != null && employeeId != -1L) {
criterias.add(criteriaBuilder.equal(rootEmployee.get(Employee_.employeeId), employeeId))
}
if (!firstName.isNullOrEmpty()) {
criterias.add(criteriaBuilder.like(rootEmployee.get(Employee_.firstName), firstName))
}
if (!lastName.isNullOrEmpty()) {
criterias.add(criteriaBuilder.like(rootEmployee.get(Employee_.lastName), lastName))
}
if (!role.isNullOrEmpty()) {
criterias.add(criteriaBuilder.like(rootEmployee.get(Employee_.role), role))
}
if (registerDate != null && registerDate != -1) {
criterias.add(criteriaBuilder.equal(rootEmployee.get(Employee_.registerDate), registerDate))
}
if (!registerClientIP.isNullOrEmpty()) {
criterias.add(criteriaBuilder.equal(rootEmployee.get(Employee_.registerClientIP), registerClientIP))
}
// if (registerUserName != null && !registerUserName.isEmpty()) {
// criterias.add(criteriaBuilder.equal(rootArUserJoin.get(ArUser_.userName), registerUserName));
// }
// if (registerUserId != null && registerUserId != -1L) {
// criterias.add(criteriaBuilder.equal(rootArUserJoin.get(ArUser_.userId), registerUserId));
// }
// val rootArUserJoin= rootEmployee.join(Employee_.current_registerUser , JoinType.INNER)
// val rootArUserFetch= rootEmployee.fetch(Employee_.current_registerUser , JoinType.INNER)
//
// val criterias = ArrayList<Predicate>()
// if (employeeId != null && employeeId != -1L) {
// criterias.add(criteriaBuilder.equal(rootEmployee.get(Employee_.employeeId), employeeId))
// }
// if (!firstName.isNullOrEmpty()) {
// criterias.add(criteriaBuilder.like(rootEmployee.get(Employee_.firstName), firstName))
// }
// if (!lastName.isNullOrEmpty()) {
// criterias.add(criteriaBuilder.like(rootEmployee.get(Employee_.lastName), lastName))
// }
// if (!role.isNullOrEmpty()) {
// criterias.add(criteriaBuilder.like(rootEmployee.get(Employee_.role), role))
// }
// if (registerDate != null && registerDate != -1) {
// criterias.add(criteriaBuilder.equal(rootEmployee.get(Employee_.registerDate), registerDate))
// }
// if (!registerClientIP.isNullOrEmpty()) {
// criterias.add(criteriaBuilder.equal(rootEmployee.get(Employee_.registerClientIP), registerClientIP))
// }
// if (registerUserName != null && !registerUserName.isEmpty()) {
// criterias.add(criteriaBuilder.equal(rootArUserJoin.get(ArUser_.userName), registerUserName));
// }
// if (registerUserId != null && registerUserId != -1L) {
// criterias.add(criteriaBuilder.equal(rootArUserJoin.get(ArUser_.userId), registerUserId));
// }
query.select(rootEmployee).where(*criterias.toTypedArray())
val q = entityManager.createQuery(query)
q.firstResult = startIndex
q.maxResults = endIndex
val results = q.getResultList()
// for (employee in results) {
// println(employee.firstName + " - " + employee.lastName )
// }
return results
}
}
<plugins>
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<version>1.8.2</version>
<executions>
<execution>
<id>schemas</id>
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
<goal>protocol</goal>
<goal>idl-protocol</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/resources/avro-schemas</sourceDirectory>
<outputDirectory>${project.basedir}/src/main/kotlin/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>${project.basedir}/src/main/resources</schemaDirectory>
<outputDirectory>${project.basedir}/src/main/kotlin</outputDirectory>
<clearOutputDir>false</clearOutputDir>
</configuration>
</plugin>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
<plugin>jpa</plugin>
<plugin>all-open</plugin>
</compilerPlugins>
<pluginOptions>
<option>all-open:annotation=javax.persistence.Entity</option>
<option>all-open:annotation=javax.persistence.Embeddable</option>
<option>all-open:annotation=javax.persistence.MappedSuperclass</option>
</pluginOptions>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-noarg</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>kapt</id>
<goals>
<goal>kapt</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>src/main/kotlin</sourceDir>
</sourceDirs>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${project.parent.version}</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>5.3.15.Final</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>2.2.4</version>
<configuration>
<outputDirectory>${project.basedir}/target/generated-sources/</outputDirectory>
</configuration>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<processors>
<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
</processors>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>5.3.15.Final</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
<configuration>
<additionalProperties>
<build.number>${buildNumber}</build.number>
</additionalProperties>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>buildnumber</id>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<format>{0,number}</format>
<items>
<item>buildNumber</item>
</items>
<doCheck>false</doCheck>
<doUpdate>false</doUpdate>
<revisionOnScmFailure>unknownbuild</revisionOnScmFailure>
</configuration>
</plugin>
</plugins>