![](/img/trans.png)
[英]MongoDB No converter found capable of converting from type [java.lang.String] to type [java.time.LocalDateTime]
[英]No converter found capable of converting from type [java.lang.String] to type [@Autowired @ManyToOne @JoinColumn com.papertrue.country.Country]
所以我對 JPA 還很陌生,並且在將一個對象映射到另一個對象時被卡住了。 這是我的用戶實體:
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;
}
我的國家實體:
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;
}
還有我的貨幣實體:
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;
}
因此,當我在 Country 對象中映射 Currency 時它可以工作,但是當我對 User 和 Country 做同樣的事情時就不行了。 我收到org.springframework.core.convert.ConverterNotFoundException
。 這是錯誤:
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
這是在我運行測試時發生的:
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 {
}
}
用戶服務服務:
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");
}
}
和參考資料庫:
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);
}
異常告訴您它無法按照@ConfigurationProperties
建議設置配置屬性。 因為對於 country 它有一個String
值(可能在application.properties
)但需要一個Country
並且沒有轉換器。
這就提出了一個問題:“你試圖用@ConfigurationProperties
注釋實現什么?”
在 JPA 實體上使用它至少是非正統的,因為 bean 和實體通常是兩個非常不同的東西。
因此,您應該刪除@ConfigurationProperties
注釋或注冊自定義轉換器,如下所述: https : //www.logicbig.com/tutorials/spring-framework/spring-boot/custom-configuration-properties-binding.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.