Hibernate how to map a String field to a numeric column

446
August 09, 2017, at 01:11 AM

I am trying to create a SpringBoot 1.5.6 application that needs to store some Entities into an Oracle 12c Database. These Entities are only available as a Maven Dependency therefore I cannot edit their code. The problem is, that these Entites use a String ID which map to an integer column by annotation, but Hibernate tries to generate a String id which fails:

org.hibernate.id.IdentifierGenerationException: Unknown integral data type for ids : java.lang.String

My approach to this problem would be to override the annotations with the orm.xml file and to tell Hibernate to use the Oracle Identity feature. However now it tries to create the Identity column as a varchar2 so I can't test if this works. I tried searching for how to specify the column definition and found "type" and "sql-type" but this does not seem to work. How can I map a String field of an Entity to an Identity column in Oracle using the orm.xml file? Can Hibernate even automatically convert an Identity to and from a String anyways?

My orm.xml so far:

<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm    
http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
    version="1.0">
    <entity class="org.owasp.appsensor.core.Attack" name="ATTACK">
        <attributes>
            <id name="id" type="string">
            <column name="id" sql-type="number"/>
                <generated-value strategy="IDENTITY" />
            </id>
        </attributes>
    </entity>
</entityMapping>

One of the Entities (which I cannot edit):

package org.owasp.appsensor.core;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.*;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.owasp.appsensor.core.rule.Rule;
import org.owasp.appsensor.core.util.DateUtils;
/**
 * An attack can be added to the system in one of two ways:
 * <ol>
 *      <li>Analysis is performed by the event analysis engine and determines an attack has occurred</li>
 *      <li>Analysis is performed by an external system (ie. WAF) and added to the system.</li>
 * </ol>
 *
 * The key difference between an {@link Event} and an {@link Attack} is that an {@link Event}
 * is "suspicous" whereas an {@link Attack} has been determined to be "malicious" by some analysis.
 *
 * @author John Melton (jtmelton@gmail.com) http://www.jtmelton.com/
 */
@Entity
public class Attack implements IAppsensorEntity {
    private static final long serialVersionUID = 7231666413877649836L;
    @Id
    @Column(columnDefinition = "integer")
    @GeneratedValue
    private String id;
    /** User who triggered the attack, could be anonymous user */
    @ManyToOne(cascade = CascadeType.ALL)
    private User user;
    /** Detection Point that was triggered */
    @ManyToOne(cascade = CascadeType.ALL)
    private DetectionPoint detectionPoint;
    /** When the attack occurred */
    @Column
    private String timestamp;
    /**
     * Identifier label for the system that detected the attack.
     * This will be either the client application, or possibly an external
     * detection system, such as syslog, a WAF, network IDS, etc.  */
    @ManyToOne(cascade = CascadeType.ALL)
    private DetectionSystem detectionSystem;
    /**
     * The resource being requested when the attack was triggered, which can be used
     * later to block requests to a given function.
     */
    @ManyToOne(cascade = CascadeType.ALL)
    private Resource resource;
    /** Rule that was triggered */
    @ManyToOne(cascade = CascadeType.ALL)
    private Rule rule;
    /** Represent extra metadata, anything client wants to send */
    @ElementCollection
    @OneToMany(cascade = CascadeType.ALL)
    private Collection<KeyValuePair> metadata = new ArrayList<>();
    public Attack () { }
    public Attack (User user, DetectionPoint detectionPoint, DetectionSystem detectionSystem) {
        this(user, detectionPoint, DateUtils.getCurrentTimestampAsString(), detectionSystem);
    }
    public Attack (User user, DetectionPoint detectionPoint, String timestamp, DetectionSystem detectionSystem) {
        setUser(user);
        setDetectionPoint(detectionPoint);
        setTimestamp(timestamp);
        setDetectionSystem(detectionSystem);
    }
    public Attack (User user, DetectionPoint detectionPoint, String timestamp, DetectionSystem detectionSystem, Resource resource) {
        setUser(user);
        setDetectionPoint(detectionPoint);
        setTimestamp(timestamp);
        setDetectionSystem(detectionSystem);
        setResource(resource);
    }
    public Attack (Event event) {
        setUser(event.getUser());
        setDetectionPoint(event.getDetectionPoint());
        setTimestamp(event.getTimestamp());
        setDetectionSystem(event.getDetectionSystem());
        setResource(event.getResource());
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public User getUser() {
        return user;
    }
    public Attack setUser(User user) {
        this.user = user;
        return this;
    }
    public DetectionPoint getDetectionPoint() {
        return detectionPoint;
    }
    public Attack setDetectionPoint(DetectionPoint detectionPoint) {
        this.detectionPoint = detectionPoint;
        return this;
    }
    public String getTimestamp() {
        return timestamp;
    }
    public Attack setTimestamp(String timestamp) {
        this.timestamp = timestamp;
        return this;
    }
    public DetectionSystem getDetectionSystem() {
        return detectionSystem;
    }
    public Attack setDetectionSystem(DetectionSystem detectionSystem) {
        this.detectionSystem = detectionSystem;
        return this;
    }
    public Resource getResource() {
        return resource;
    }
    public Attack setResource(Resource resource) {
        this.resource = resource;
        return this;
    }
    public Rule getRule() {
        return this.rule;
    }
    public Attack setRule(Rule rule) {
        this.rule = rule;
        return this;
    }
    public Collection<KeyValuePair> getMetadata() {
        return metadata;
    }
    public void setMetadata(Collection<KeyValuePair> metadata) {
        this.metadata = metadata;
    }
    @Override
    public int hashCode() {
        return new HashCodeBuilder(17,31).
                append(user).
                append(detectionPoint).
                append(timestamp).
                append(detectionSystem).
                append(resource).
                append(metadata).
                toHashCode();
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Attack other = (Attack) obj;
        return new EqualsBuilder().
                append(user, other.getUser()).
                append(detectionPoint, other.getDetectionPoint()).
                append(timestamp, other.getTimestamp()).
                append(detectionSystem, other.getDetectionSystem()).
                append(resource, other.getResource()).
                append(metadata, other.getMetadata()).
                isEquals();
    }
    @Override
    public String toString() {
        return new ToStringBuilder(this).
                   append("user", user).
                   append("detectionPoint", detectionPoint).
                   append("rule", rule).
                   append("timestamp", timestamp).
                   append("detectionSystem", detectionSystem).
                   append("resource", resource).
                   append("metadata", metadata).
                   toString();
    }
}

This produces the following error when trying to create the schema:

Hibernate: create table AS_ATTACK (id varchar2(255 char) generated as identity, timestamp varchar2(255 char), detection_point_id varchar2(255 char), detection_system_id varchar2(255 char), resource_id varchar2(255 char), rule_id varchar2(255 char), user_id varchar2(255 char), primary key (id))
14:06:42.880 [main] ERROR o.h.t.h.SchemaExport - HHH000389: Unsuccessful: create table AS_ATTACK (id varchar2(255 char) generated as identity, timestamp varchar2(255 char), detection_point_id varchar2(255 char), detection_system_id varchar2(255 char), resource_id varchar2(255 char), rule_id varchar2(255 char), user_id varchar2(255 char), primary key (id))
 - 14:06:42.881 [main] ERROR o.h.t.h.SchemaExport - ORA-00604: Fehler auf rekursiver SQL-Ebene 1 (= Error on recursive SQL-Level 1)
ORA-06502: PL/SQL: numerischer oder Wertefehler (= numerical or value error)
ORA-06512: in Zeile 17 (= in line 17)
ORA-30675: Identity-Spalte muss einen numerischen Typ aufweisen (= Identity-column must have a numerical type)
Rent Charter Buses Company
READ ALSO
Signing Java desktop application

Signing Java desktop application

I made a Java desktop application using Swing (myProgjar)

362
Java- Add multi attachment to email using MimeMessageHelper

Java- Add multi attachment to email using MimeMessageHelper

I'm trying to add more than one file attachment to email, have this method used to prepare attachment, it will work for two files not the third

643
Track someone&#39;s location via phone number or IMEI [on hold]

Track someone's location via phone number or IMEI [on hold]

I am currently working on an android app project for the command center, I am confused after searching how to track someone's location via IMEI or mobile phone number accurately in Android, I wanna ask is there any way to make such thing? Thanks

316
something blocks cxf using tunnel, 503 forwarding failure

something blocks cxf using tunnel, 503 forwarding failure

I can't solve timeout errorI suppose it is something with security,

351