Hibernate 5.1为Oracle SYS.XMLTYPE实现UserType

时间:2017-05-29 13:41:47

标签: hibernate jpa xmltype hibernate-5.x

我正在尝试将Oracle XMLTYPE数据存储为我的java应用程序中的JPA Entity。首先,我为xml字段实现了UserType:

    public class FiledsListUserType implements UserType {

    private static JAXBContext jaxbContext;

    public Object nullSafeGet(final ResultSet resultSet, final String[] names, final SessionImplementor session, final Object owner) throws SQLException {
        XMLType xmlType = (XMLType) resultSet.getObject(names[0]);

        ...

但问题是我在nullSafeGet resultSet.getObject(names[0])中得到null。我想如果发生了,因为我使用EntityManager而不是Session,但现在它似乎没有。我还注意到其他字段是从Oracle成功获得的。这是我的实体:

@Entity
public class Application implements Serializable{
...
   @Type(type = "fieldsList")
   @Column(name = "XMLOTHERINFORMATION")
   private FieldsList xmlOtherInformation;
...


@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "list")
public class FieldsList {

    @XmlElement(name = "Field")
    private List<Field> fields = new ArrayList<Field>();

...

@XmlAccessorType(XmlAccessType.FIELD)
public class Field {

    @XmlAttribute
    private String id;
    @XmlElement
    private String fldCode;
...

正如文档所述,我按如下方式注册了我的UserType:

public class OracleXmlDialect extends Oracle10gDialect {

    public OracleXmlDialect() {
        registerColumnType(XMLType._SQL_TYPECODE, "XMLTYPE");
    }

    @Override
    public void contributeTypes(final TypeContributions typeContributions, final ServiceRegistry serviceRegistry) {
        super.contributeTypes(typeContributions, serviceRegistry);
        try {
            registerTypes(typeContributions);
        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void registerTypes(final TypeContributions typeContributions) throws JAXBException {
        typeContributions.contributeType(new FiledsListUserType(), "fieldsList");
    }

那么我错过了什么?

0 个答案:

没有答案