我需要使用 Hibernate 將 BigDecimal[][] 型別持久保存到 MySQL 資料庫。我不知道該怎么做。我試過這個:
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Embeddable
@TypeDef(name = "coordinates_array",
typeClass = BigDecimalArrayType.class)
public class Geometry {
private String type;
@Type(
type = "coordinates_array",
parameters = @org.hibernate.annotations.Parameter(
name = "sql_array_type",
value = "BigDecimal"
)
)
@Column(
name = "coordinates",
columnDefinition = "BigDecimal[][]"
)
BigDecimal[][] coordinates;}
沒有 BigDecimalArrayType 類。如何定義 BigDecimalArrayType?或者還有什么我們可以代替的嗎?
uj5u.com熱心網友回復:
正如我在評論中提到的,由于您不想以“純文本”方式保存陣列,因此您可以按照這種方式進行操作。
使用兩個欄位,一個是持久的(資料庫的一部分),另一個是瞬態的(僅是應用程式邏輯的一部分)。Persistent 將是byte[]您的CoordinateHolder,瞬態將是BigDecimal[][].
這是CoordinateHolder. 如您所見,我正在使用@Transient禁止保留欄位。
@Embeddable
public class CoordinateHolder implements Serializable
{
@Transient
private BigDecimal[][] transientCoordinates; // Will not be saved in the DB, is part only of the business logic.
public BigDecimal[][] getTransientCoordinates()
{
return transientCoordinates;
}
public void setTransientCoordinates(BigDecimal[][] transientCoordinates)
{
this.transientCoordinates = transientCoordinates;
}
}
在這之后讓我們看一下物體類:
@Entity(name = "test_name")
public class TestEntity
{
// Your id...
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Embedded
private CoordinateHolder coordinateHolder;
@Lob
@Column(name = "coordinates", columnDefinition = "BLOB")
private byte[] blobCoordinates;
public void setCoordinates(BigDecimal[][] arr)
{
if (coordinateHolder == null)
{
coordinateHolder = new CoordinateHolder();
}
coordinateHolder.setTransientCoordinates(arr);
}
public BigDecimal[][] getCoordinates()
{
if (coordinateHolder == null)
{
return null;
}
return coordinateHolder.getTransientCoordinates();
}
// This can also be done through Entity listeners which actually do the same thing
// Pre persist is always performed before persisting this entity
@PrePersist
public void prePersisting()
{
System.out.println("Hey I am being persisted. And now I am turning my transient coordinates into BLOB for the db");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out;
try
{
out = new ObjectOutputStream(bos);
out.writeObject(coordinateHolder);
out.flush();
blobCoordinates = bos.toByteArray();
} catch (Exception e)
{
// Do smth with your exception...
e.printStackTrace();
} finally
{
try
{
bos.close();
} catch (IOException ignored)
{
// ignore close exception
}
}
}
// Perform after loading...
@PostLoad
public void postLoading()
{
System.out.println("Hey I am being fetched, I am turning my BLOB into the array holder so that you can use it in your app");
ByteArrayInputStream bis = new ByteArrayInputStream(this.getBlobCoordinates());
ObjectInput in = null;
try
{
in = new ObjectInputStream(bis);
CoordinateHolder o = (CoordinateHolder) in.readObject();
this.setCoordinateHolder(o);
}
catch (Exception e)
{
// Do smth with your exception...
e.printStackTrace();
}
finally
{
try
{
if (in != null)
{
in.close();
}
} catch (IOException ex)
{
// ignore close exception
}
}
}
... Further getters and setters...
}
代碼是用注釋描述的,應該是可以理解的。這使您可以堅持所需的內容。
uj5u.com熱心網友回復:
如果您想保持原樣,也許您可??以將 BigDecimal[][] 轉換為 blob,而不是將行/列分解到適當的表中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/440784.html
上一篇:如何在方法結束時關閉連接
