我有一个新的SpringBoot应用程序来公开一些与jar中的实体相关的服务(作为一个依赖项包含在POM中)。对于数据访问,我计划使用SpringData,这样我就可以使用伟大的JpaRepository,而不是手动编写DAO。
jar在代码中是可见的,所以编译一切都很好,但是当Spring开始连接bean时,它会抛出异常:
Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.companyName.common.countrytype.Country
at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:210) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final]
at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:70) ~[spring-data-jpa-1.10.3.RELEASE.jar:na]
在JAR中声明@Entity并使用SpringData/jpaEntity时,您是否发现了我缺少的地方?你做过类似的事吗?
这是我的代码:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>country-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>country-service</name>
<description>Spring Boot country-service</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-ws</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>mycompany-core</artifactId>
<version>1.1.20</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.mycompany.admin.api;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@EnableJpaRepositories(basePackages={"com.mycompany.admin.api"}) //tried this but does not work
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
package com.mycompany.admin.api;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import com.mycompany.common.countrytype.Country; //This is located in a jar included as dependency in the pom
public interface CountryRepository extends JpaRepository<Country, Long> {
}
package com.mycompany.admin.api;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.mycompany.common.countrytype.Country;
@Controller
public class CountryController {
@Autowired
private CountryRepository repo;
@RequestMapping("/")
@ResponseBody
public String greeting() {
return "Hello";
}
@RequestMapping("/countries")
@ResponseBody
public String listCountry() {
List<Country> countries;
try {
countries = repo.findAll();
} catch (Exception e) {
System.out.println(e.getMessage());
return e.getMessage();
}
if (countries.isEmpty()) {
String errorMst = "no countries found";
System.out.println(errorMst);
return errorMst;
} else {
return "size:" + countries.size();
}
}
}
package com.mycompany.admin.api;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
@Entity
@Table(name = "country", uniqueConstraints = { @UniqueConstraint(columnNames = "code"), @UniqueConstraint(columnNames = "name") })
public class Country {
protected Long id;
private String code;
private String name;
protected Country() {
}
public Country(Long id, String code, String name) {
this.id = id;
this.code = code;
this.name = name;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "code", unique = true, nullable = false, length = 2)
public String getCode() {
return this.code;
}
public void setCode(String code) {
this.code = code;
}
@Column(name = "name", unique = true, nullable = false, length = 100)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}
Country country = (Country) o;
if (!code.equals(country.code)) {
return false;
}
if (!name.equals(country.name)) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + code.hashCode();
result = 31 * result + name.hashCode();
return result;
}
}
提前感谢!
使用@entityscan(“your.lib.package.with.entities”)
我试图合并2个简单的程序。我想制作成一个。jar文件,这是我编写的代码:(Project1.jar和Project2.exe都打包到这个。jar中) 当我在eclipse中运行项目时,一切都很好(两个文件都像我想要的那样被执行)。但是当我将这个项目导出到。jar并运行它时,什么也没有发生。我认为它与文件路径有关,因为它在Eclipse中运行时工作得很好。怎么解决这个?
我有一个我写的程序的输出文件。它由FileWriter和BufferedWriter编写。 我是否错误地使用了FileWriter/BufferedWriter,或者它在.jar文件中不起作用?
我希望使用Maven在任何平台上使用JavaFX执行jar,无论主机上是否安装了JavaFX。
nav.component.ts app.module.ts app.component.ts
问题内容: 我正在用Java开发游戏,我想将随机生成的地图保存在图像上,然后加载它。我的代码在Eclipse中工作正常,但是当我将其导出到.jar / .exe文件时,它在制作文件(“ mapf”)时会遇到问题。谢谢您的回答。 堆栈跟踪: 问题答案: 您似乎认为您可以将jar视为目录结构,事实并非如此。您甚至都不应该 考虑 将代码从中运行到jar文件中(可能的话,但是会涉及 很多 陷阱)。 假设您