开发者

How to get a JPA-persisted String[] column by ResultSet#getString() in JDBC?

开发者 https://www.devze.com 2023-04-08 01:17 出处:网络
I want to store multiple checkbox values in a single DB column. I don\'t want to store in a seperate table with @OneToMany.

I want to store multiple checkbox values in a single DB column. I don't want to store in a seperate table with @OneToMany.

This is the JPA entity:

@Entity
@Table(name="COOK")
...
@Column(name="ARRAY_FOOD" )
private String[] arrayFood;
...getter and setters...

Here are the checkbox values (JSF):

<h:selectManyCheckbox value="#{foodBean.selectedModel.arrayFood}" layout="lineDirection">
    <f:selectItem itemLabel="FISH"       itemValue="FISH"           />
    <f:selectItem itemLabel="SOUP"       itemValue="SOUP"           />
    <f:selectItem itemLabel="CAKE"       itemValue="CAKE"           />
    <f:selectItem itemLabel="1/2 WATER"  itemValue="1/2 WATER"      />
    <f:selectItem itemLabel="BANANA 1KG" itemValue="BANANA 1KG"     />
</h:selectManyCheckbox>

JPA stores the submitted checked values as follows:

Column Name Data Typ开发者_开发百科e   Sample Data
----------------------------------------
ARRAY_FOOD  RAW (255)   ACED0005757200135B4C6A6176612E6C616E672E537472696E673BADD256E7E91D7B4702000078700000000174000141

How can I get the original array by ResultSet in aother program?

String array = rs.getString("ARRAY_FOOD");  // ???????????

Or how can I format it in a understandable format like so in other program I can split it by char like ";" :

FISH;SOUP;BANANA 1KG


By default serializable types that are not supported database types are serialized. You would need to use Java serialization to get these types back outside of JPA.

You could instead use a @Converter to convert the array as you wish if using EclipseLink.

You could also use get/set method to convert the value, see,

http://en.wikibooks.org/wiki/Java_Persistence/Basic_Attributes#Conversion

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号