gawi
gawi

Reputation: 2962

Hibernate annotation for base java class

I would like to put into db a class that have java.awt.geom.Point2D field. Is it possible?

Here is my code.

@Entity
@Table(name = "my_class_table")
public class MyClass {
  private String aliasId;

  private Point2D field;

  public Point2D getField() {
    return field;
  }

  public void setFieldPoint2D field) {
    this.field = field;
  }

  public String getAliasId() {
    return aliasId;
  }

  public void setAliasId(String aliasId) {
    this.aliasId = aliasId;
  }
}

And the reason of the exception which is thrown:

Could not determine type for: java.awt.geom.Point2D, at table: my_class_table, for columns: [org.hibernate.mapping.Column(field)]

Of course, the reason is quite obvious. My question is: how should I annotate the class to be able to use a field of Point2D class? Is it possible at all?

Upvotes: 1

Views: 317

Answers (3)

dcernahoschi
dcernahoschi

Reputation: 15250

The simplest way is to use a java.awt.Point that extends Point2D and is a Serializable class. This way hibernate will automatically map it with SerializableType and you don't need to do anything more. The point object will be saved in its serialized form in a blob database table column.

You have also the option to define a custom hibernate type for the Point2D class. Here is a link of how to define a custom hibernate type.

Upvotes: 1

gawi
gawi

Reputation: 2962

Thanks guys for response. Unfortunatelly java.awt.Point class uses Integer, so it is useless in my case. The easiest way to solve it would be to use Point2D.Double which implements Serializable (but definition of UserType or CompositeUserType is more convenient if you don't want to change class definition). So, the simple solution:

@Entity
@Table(name = "my_class_table")
public class MyClass {
  private String aliasId;

  private Point2D.Double field;

  public Point2D.Double getField() {
    return field;
  }

  public void setField(Point2D.Double field) {
    this.field = field;
  }

  public String getAliasId() {
    return aliasId;
  }

  public void setAliasId(String aliasId) {
    this.aliasId = aliasId;
  }
}

But my final goal was to create a class with ordered list of points. If anybody is interested here is an example of the class representing line:

@Entity
public class Line {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "idDb", unique = true, nullable = false)
    private int id;

    @ElementCollection
    @CollectionTable(name="points_table", joinColumns = @JoinColumn(name="idDb"))
    @IndexColumn(name = "idx")
    @Column(name="point_val")
    private List<Point2D.Double> points = new ArrayList<Point2D.Double>();

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public List<Point2D.Double> getPoints() {
        return points;
    }

    public void setPoints(List<Point2D.Double> points) {
        this.points = points;
    }
}

Upvotes: 0

Joachim Sauer
Joachim Sauer

Reputation: 308269

You can't add annotations to existing classes.

But you can define a CompositeUserType to tell Hibernate how to map a Point2D.

Upvotes: 0

Related Questions