[英]No converter found capable of converting from type [java.lang.String] to type [@Autowired @ManyToOne @JoinColumn com.papertrue.country.Country]
So I'm pretty new to JPA and I'm stuck while mapping an object into another object.所以我对 JPA 还很陌生,并且在将一个对象映射到另一个对象时被卡住了。 Here's my User Entity:
这是我的用户实体:
package com.papertrue.user;
import com.papertrue.country.Country;
import com.papertrue.currency.Currency;
@Entity
@ConfigurationProperties("user")
@Table(name = "users")
public class User {
@Id
@Column(name = "id")
private String userId;
private String name;
@Column(name = "phone_ext")
private String phoneExt;
@Column(name = "phone_no")
private String phoneNo;
private String email;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "country")
private Country country;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "currency")
private Currency currency;
@Column(name = "fb_id")
private String facebookId;
@Column(name = "google_id")
private String googleId;
@Column(name = "current_balance")
private int currentBalance;
@Column(name = "is_enabled")
private boolean isEnabled;
@Column(name = "free_sample_used")
private boolean isfreeSampleUsed;
@Column(name = "client_id")
private String clientId;
}
My Country Entity:我的国家实体:
package com.papertrue.country;
import com.papertrue.currency.Currency;
@Entity
@ConfigurationProperties("country")
@Table(name = "countries")
public class Country {
@Id
private String code;
private String name;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "currency")
private Currency currency;
@Column(name = "isd_ext")
private String ISDExt;
@Enumerated(EnumType.STRING)
private transient Region region;
}
And my Currency Entity:还有我的货币实体:
package com.papertrue.currency;
@Entity
@ConfigurationProperties("currency")
@Table(name = "currencies")
public class Currency {
@Id
private String code;
private String symbol;
private String name;
private int multiplier;
@Column(name = "minVal")
private int minValue;
}
So it's working when I am mapping Currency with-in the Country object but it's somehow not when I do the same thing with User and Country.因此,当我在 Country 对象中映射 Currency 时它可以工作,但是当我对 User 和 Country 做同样的事情时就不行了。 I'm getting
org.springframework.core.convert.ConverterNotFoundException
.我收到
org.springframework.core.convert.ConverterNotFoundException
。 Here's the error:这是错误:
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132) ~[spring-test-5.3.0.jar:5.3.0]
Caused by: org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'user-com.papertrue.user.User': Could not bind properties to 'User' : prefix=user, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'user.country' to com.papertrue.country.Country
... 70 common frames omitted
Caused by: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'user.country' to com.papertrue.country.Country
... 89 common frames omitted
Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [@javax.persistence.ManyToOne @javax.persistence.JoinColumn com.papertrue.country.Country]
... 105 common frames omitted
And this is happening while I'm running the test:这是在我运行测试时发生的:
package com.papertrue;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import com.papertrue.exceptions.PaperTrueUserNotFoundException;
import com.papertrue.user.UserService;
@SpringBootTest
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void contextLoads() {
// Testing getEmailInstance
System.out.println("====== getEmailInstance() ======");
try {
System.out.println(userService.getEmailInstance("text@papertrue.com").getName());
} catch (PaperTrueUserNotFoundException e) {
e.printStackTrace();
}
}
@SpringBootApplication
static class TestConfiguration {
}
}
The UserService Service:用户服务服务:
package com.papertrue.user;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import com.papertrue.exceptions.PaperTrueUserNotFoundException;
@Service
@EnableConfigurationProperties(User.class)
public class UserService {
@Autowired
private UserRepository userRepository;
/**
* Returns user whose email matches to parameter email's value.
*
* @param email
* @return
* @throws PaperTrueUserNotFoundException
*/
public User getEmailInstance(String email) throws PaperTrueUserNotFoundException {
Optional<User> optional = userRepository.findByEmail(email);
if (optional.isPresent()) {
return optional.get();
}
throw new PaperTrueUserNotFoundException("User Not Found");
}
}
And the Repository for reference:和参考资料库:
package com.papertrue.user;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, String> {
Optional<User> findByEmail(String email);
}
The exception tells you that it can't set the configuration properties as advised by @ConfigurationProperties
.异常告诉您它无法按照
@ConfigurationProperties
建议设置配置属性。 Because for country it has a String
value (possibly in the application.properties
) but needs a Country
and doesn't have a converter for that.因为对于 country 它有一个
String
值(可能在application.properties
)但需要一个Country
并且没有转换器。
This brings up the question: "What do you try to achieve with the @ConfigurationProperties
annotations?"这就提出了一个问题:“你试图用
@ConfigurationProperties
注释实现什么?”
It is at least unorthodox to use it on JPA entities, because beans and entities are normally two very different things.在 JPA 实体上使用它至少是非正统的,因为 bean 和实体通常是两个非常不同的东西。
So either you should remove the @ConfigurationProperties
annotations or register a custom converter as described here: https://www.logicbig.com/tutorials/spring-framework/spring-boot/custom-configuration-properties-binding.html因此,您应该删除
@ConfigurationProperties
注释或注册自定义转换器,如下所述: https : //www.logicbig.com/tutorials/spring-framework/spring-boot/custom-configuration-properties-binding.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.