英文:
JAXB: get Object from DB by one of it field from xml
问题
I think you're asking how to correctly retrieve a Country
object from the database in your CountryISO2Adapter
class, which is used for unmarshalling XML data. To achieve this, you should use a proper approach to access your Service
class. However, it seems you've encountered issues with @Autowired
.
One possible solution is to avoid using @Autowired
in the CountryISO2Adapter
class because it's not managed by Spring. Instead, you can manually obtain the Service
bean from the Spring context. Here's an example of how you can do this:
@Component
public class CountryISO2Adapter extends XmlAdapter<String, Country> {
@Override
public Country unmarshal(String v) throws Exception {
ApplicationContext context = ApplicationContextProvider.getApplicationContext();
Service service = context.getBean(Service.class);
return service.getCountryByISO2(v).orElse(null);
}
// ...
}
You would also need to create an ApplicationContextProvider
class to access the Spring application context:
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class ApplicationContextProvider implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
context = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return context;
}
}
With these changes, your CountryISO2Adapter
should be able to retrieve the Service
bean and access the database to fetch the Country
object correctly.
英文:
I think that I have a trivial problem but cant find solution.
I have Class with some fields of another Classes. In XML I have one of fields from "Another Class". How can I get Another Class object by this field?
I Using SpringBoot.
Short example (Main class - Profile, Another class - Country):
XML
<?xml version="1.0" encoding="UTF-8"?>
<profiles>
<profile id="1001">
...
<nationality>US</nationality>
...
</profile>
</profiles>
Main class
@Entity
@Table
@XmlRootElement(name = "profile")
@XmlAccessorType(XmlAccessType.FIELD)
public class Profile {
...
@ManyToOne
@JoinColumn(referencedColumnName = "numericISOCode")
@XmlElement(name = "nationality")
@XmlJavaTypeAdapter(CountryISO2Adapter.class)
private Country nationality;
...
Country class
@Entity
@Table(uniqueConstraints = {
@UniqueConstraint(name = "alpha2ISO", columnNames = {"alpha2ISO"}),
@UniqueConstraint(name = "alpha3ISO", columnNames = {"alpha3ISO"})})
public class Country {
private String nameENG;
@Column(unique = true, nullable = false, length = 2)
private String alpha2ISO;
@Column(unique = true, nullable = false, length = 3)
private String alpha3ISO;
@Id
@Column(unique = true, nullable = false, length = 3)
private int numericISOCode;
...
Adapter
@Component
public class CountryISO2Adapter extends XmlAdapter<String, Country> {
@Autowired
private Service service;
@Override
public Country unmarshal(String v) throws Exception {
return service.getCountryByISO2(v).orElse(null);
}
....
Service
@Service
public final class Service {
...
private final CountryRepository countryRepository;
...
public Optional<Country> getCountryByISO2 (String iso2){
return countryRepository.findByAlpha2ISO(iso2);
}
I tried get it from Adapter.class where I want looking it by means of methods @Service class and @Repository, but in Adapter class than I need link of bean @Servise class but @Autowired not worked in this case (may be @XmlJavaTypeAdapter get "new CountryISO2Adapter()" and it can't using bean) - I have null in my Service field in Adapter class;
May be that's wrong way? How make this correctly?
Global target - create object Profile.class with field Country.class, however Country.class object I must get from DB (but not create).
I'm sure it's a often task.
Thanks a lot for your advises.
答案1
得分: 0
以下是翻译好的部分:
"似乎不是一个好主意将DTO和实体类混合在一起。在我看来,更好的解决方案是创建两个单独的类:一个带有XML注解的DTO类,一个带有JPA注解的实体类,然后通过单独的转换器类将它们转换成另一个类,该转换器类将是一个Spring组件,并自动装配countryRepository以查找必要的值。"
英文:
It seems like not good idea to mix dto and entity classes. In my opinion, the better solution is to create two separate classes: dto class with xml annotations and entity class with jpa annotations and convert them into another through separate converter class which will be a spring component and autowired countryRepository to find necessary value
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论