从XML中通过其中一个字段获取对象的方式:JAXB。

huangapple go评论85阅读模式
英文:

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;profiles&gt;
    &lt;profile id=&quot;1001&quot;&gt;
        ...
        &lt;nationality&gt;US&lt;/nationality&gt;
        ...
    &lt;/profile&gt;
&lt;/profiles&gt;

Main class

@Entity
@Table
@XmlRootElement(name = &quot;profile&quot;)
@XmlAccessorType(XmlAccessType.FIELD)
public class Profile {
...
@ManyToOne
@JoinColumn(referencedColumnName = &quot;numericISOCode&quot;)
@XmlElement(name = &quot;nationality&quot;)
@XmlJavaTypeAdapter(CountryISO2Adapter.class)
private Country nationality;
...

Country class

@Entity
@Table(uniqueConstraints = {
        @UniqueConstraint(name = &quot;alpha2ISO&quot;, columnNames = {&quot;alpha2ISO&quot;}),
        @UniqueConstraint(name = &quot;alpha3ISO&quot;, columnNames = {&quot;alpha3ISO&quot;})})
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&lt;String, Country&gt; {
    @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&lt;Country&gt; 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

huangapple
  • 本文由 发表于 2023年6月27日 19:40:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定