我想在Spring Java配置中创建一个Spring bean,并在运行时传递一些构造函数参数.我创建了以下Java配置,其中有一个bean fixedLengthReport,它需要构造函数中的一些参数.
@Configuration
public class AppConfig {
@Autowrire
Dao dao;
@Bean
@Scope(value = "prototype")
**//SourceSystem can change at runtime**
public FixedLengthReport fixedLengthReport(String sourceSystem) {
return new TdctFixedLengthReport(sourceSystem, dao);
}
}
Run Code Online (Sandbox Code Playgroud)
但我收到的错误是sourceSystem无法连接,因为没有找到bean.如何使用运行时构造函数参数创建bean?
我使用的是Spring 4.2
我有以下表格如何将它们映射到JPA实体:
TABLE Event {
EventID
SourceID
....Other event fields
PK (EventID, SourceID)
}
TABLE MEETING {
MeetingID
EventID
SourceID
...Other meeting fields
PK(MeetingID,EventID, SourceID)
FK(EventID, SourceID) //FK with Event table
}
Run Code Online (Sandbox Code Playgroud)
Event表与Meeting表具有一对多的关系.我如何在JPA中映射这种双向关系?
如何从spring rest模板中获取原始json字符串?我试过跟随代码,但它返回我没有引号的json导致其他问题,我怎么能得到json原样.
ResponseEntity<Object> response = restTemplate.getForEntity(url, Object.class);
String json = response.getBody().toString();
Run Code Online (Sandbox Code Playgroud) 我得到一个大的json文档,我想只解析它的一部分到我的java类.我正在考虑使用类似jsonpath的东西从中提取部分数据,而不是创建整个java类的层次结构.
Jackson或Gson是否以任何方式支持jsonpath?如果是,您可以为我提供一些示例或指向另一个标准库吗?
例如,假设我有一个下面的文档,我想在我的java类中仅提取它下面的数据:
$ .store.book [0] - 只有第一本书$ .store.bicycle.price - 自行车的价格
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": …
Run Code Online (Sandbox Code Playgroud) 我正在将Spring数据与JPA 2.1结合使用,使用以下方法检索具有分页和规范(条件构建器)的实体:
Page<T> findAll(Specification<T> spec, Pageable pageable)
Run Code Online (Sandbox Code Playgroud)
如何指定@EntityGraph
此方法?现在,如果我用注释@EntityGraph
,Spring将停止分页并一次性获取整个数据。
我在 Postgres 数据库中使用 JPA(休眠),我的休眠数据库配置如下:
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
properties.setProperty("hibernate.hbm2ddl.auto","validate");
Run Code Online (Sandbox Code Playgroud)
我有一张带财产的桌子 created_by_user_id
CREATE TABLE dppm_main.user_account_profile
( ....//other columns
created_by_user_id integer
)
Run Code Online (Sandbox Code Playgroud)
映射到 JPA 实体如下:
@Column(name = "created_by_user_id")
private Long createdByUserId;
Run Code Online (Sandbox Code Playgroud)
在进行架构验证时,我收到以下错误:
org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column
type encountered in column [created_by_user_id] in table [user_account_profile];
**found [int4 (Types#INTEGER)], but expecting [int8 (Types#BIGINT)]**
Run Code Online (Sandbox Code Playgroud)
我该如何解决?我在很多地方都遇到了这个问题,所以是否可以通过扩展PostgreSQL82Dialect
而不是columnDefinition来修复它。
我有以下 DTO 和域对象。我正在使用 Mapstruct 将域对象复制到 DTO 对象。
public class AddressDomain {
private String street;
private Telephone telephone;
}
public class CompanyDomain{
private String id;
private Address address;
}
public class AddressDTO {
private String street;
private Telephone telephone;
}
public class CompanyDTO{
private String id;
private Address address;
}
Run Code Online (Sandbox Code Playgroud)
使用下面的映射器将域映射到 DTO。我不想将电话属性从域映射到 DTO。怎么做?我尝试在映射忽略中提供嵌套目标属性,但它给出了错误:
public interface CompanyMapper {
//**below line gives error**
@Mapping(target = "address.telephone", ignore=true)
CompanyDTO map(AddressDTO dto);
}
Run Code Online (Sandbox Code Playgroud) 我有一个数字,格式为(13,2)13位和2位小数.我需要对它进行一些计算(比如乘法,除法).我打算BigDecimal
用于计算.我应该使用double或float进行计算,因为BigDecimal
比较慢一点?
java ×6
jpa ×3
spring ×3
hibernate ×2
json ×2
bigdecimal ×1
gson ×1
jackson ×1
mapstruct ×1
postgresql ×1
spring-bean ×1
spring-data ×1
spring-mvc ×1
spring-rest ×1