I have a lot of hbm files that I need to convert to orm.xml. I’m trying to use the gradle plugin, but I don’t understand how to use it.
I also have tried this aproach :
package org.example;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
import org.hibernate.boot.jaxb.hbm.transform.HbmXmlTransformer;
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping;
import org.hibernate.boot.jaxb.mapping.JaxbEntityMappings;
import org.hibernate.boot.jaxb.Origin;
import java.io.File;
import java.io.FileOutputStream;
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
public static void main(String[] args) {
System.setProperty("javax.xml.accessExternalDTD", "all");
String hbmXmlFilePath = "C:/Users/filipe.gomes.arrais/IdeaProjects/hbmOrmConverter/src/main/Hbm/IrGroup.hbm.xml";
// Carregar o arquivo .hbm.xml
File hbmXmlFile = new File(hbmXmlFilePath);
// Carregar o arquivo .hbm.xml em um objeto JaxbHbmHibernateMapping
JaxbHbmHibernateMapping hbmXmlMapping = load(hbmXmlFile);
Origin unknownOrigin = new Origin(null, "<unknown>");
// Transformar o objeto JaxbHbmHibernateMapping em um objeto JaxbEntityMappings
JaxbEntityMappings ormXmlMapping = HbmXmlTransformer.transform(hbmXmlMapping, unknownOrigin, null);
// Salvar o objeto JaxbEntityMappings em um arquivo .orm.xml
String outputFilePath = hbmXmlFilePath.replace(".hbm.xml", ".orm.xml");
write(ormXmlMapping, outputFilePath);
ormXmlMapping.toString();
System.out.println("Arquivo .orm.xml gerado com sucesso em: " + outputFilePath);
}
private static JaxbHbmHibernateMapping load(File file) {
try {
JAXBContext context = JAXBContext.newInstance(JaxbHbmHibernateMapping.class);
return (JaxbHbmHibernateMapping) context.createUnmarshaller().unmarshal(file);
} catch (JAXBException e) {
e.printStackTrace();
return null;
}
}
private static void write(JaxbEntityMappings ormMapping, String filePath) {
try {
JAXBContext context = JAXBContext.newInstance(JaxbEntityMappings.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(ormMapping, new FileOutputStream(filePath));
} catch (Exception e) {
e.printStackTrace();
}
}
}
but didnt work i got this error:
jakarta.xml.bind.UnmarshalException: unexpected element (uri:“”, local:“hibernate-mapping”). Expected elements are <{Index of /xsd/orm/hbm - Hibernate}hibernate-mapping>