Created
February 26, 2025 19:03
-
-
Save MSaifAsif/02cbe65a3f90d118f9c80b1aa0d9fb2f to your computer and use it in GitHub Desktop.
Class to support storing XML strings directly into an XML database column type.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.types; | |
| import org.hibernate.engine.spi.SharedSessionContractImplementor; | |
| import java.io.Serializable; | |
| import java.sql.PreparedStatement; | |
| import java.sql.ResultSet; | |
| import java.sql.SQLException; | |
| import java.sql.Types; | |
| /** | |
| * Class to support storing XML strings directly into an XML database column type. | |
| * | |
| * The following code snippet assumes that | |
| * <ul> | |
| * <li>You are using jakarta.persistence annotations</li> | |
| * <li>The column `xmlColumn` is created as type xml for your database</li> | |
| * <li>You want to store an XML string in this column directly. You can use your own type as required, this example uses String</li> | |
| * </ul> | |
| * <pre> | |
| * @jakarta.persistence.Entity | |
| * class MyEntity { | |
| * | |
| * @jakarta.persistence.Id | |
| * private Long Id; | |
| * | |
| * // various attributes for other columns | |
| * | |
| * @org.hibernate.annotations.Type(SqlXmlType.class) | |
| * private String xmlColumn; | |
| * | |
| * // getter/setters | |
| * } | |
| * </pre> | |
| * And in order to use this entity | |
| * | |
| * <pre> | |
| * var object = new MyEntity(); | |
| * // set other fields as required | |
| * String xmlString = "<head><body>custom</body></head>" | |
| * object.setXmlColumn(xmlString); | |
| * repository.save(object); | |
| * </pre> | |
| * | |
| * @author msaifasif | |
| * @version 1.0.0 | |
| * @see org.hibernate.usertype.UserType | |
| */ | |
| public class SqlXmlType implements org.hibernate.usertype.UserType<String> { | |
| private final int sqlTypeSupported = Types.LONGNVARCHAR; | |
| @Override | |
| public int getSqlType() { | |
| return sqlTypeSupported; | |
| } | |
| @Override | |
| public Class<String> returnedClass() { | |
| return String.class; | |
| } | |
| @Override | |
| public boolean equals(String s, String j1) { | |
| return s.equals(j1); | |
| } | |
| @Override | |
| public int hashCode(String s) { | |
| return s.hashCode(); | |
| } | |
| @Override | |
| public String nullSafeGet(ResultSet resultSet, int position, SharedSessionContractImplementor sharedSessionContractImplementor, Object o) throws SQLException { | |
| String xml = resultSet.getString(position); | |
| return resultSet.wasNull() ? null : xml; | |
| } | |
| @Override | |
| public void nullSafeSet(PreparedStatement preparedStatement, String value, int index, SharedSessionContractImplementor sharedSessionContractImplementor) throws SQLException { | |
| if (value == null) { | |
| preparedStatement.setNull(index, Types.OTHER); | |
| } else { | |
| preparedStatement.setObject(index, value, Types.OTHER); | |
| } | |
| } | |
| @Override | |
| public String deepCopy(String s) { | |
| return ""; | |
| } | |
| @Override | |
| public boolean isMutable() { | |
| return false; | |
| } | |
| @Override | |
| public Serializable disassemble(String s) { | |
| return (String) s; | |
| } | |
| @Override | |
| public String assemble(Serializable serializable, Object o) { | |
| return (String) serializable; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment