我想将字符实体存储在H2数据库中。该字母实体的属性是Java字符对象。
一切都按预期进行。我只是有一个问题,我无法使用Java PreparedStatement插入带有空格字符(“ ASCII码32dec”)的实体。插入时总会以某种方式在输入字符串上调用修剪,导致字符在数据库中存储为“''。
我的插入代码如下:
PreparedStatement statement =
context.getConnection().prepareStatement("insert into DLM.ALPHABET_CHARACTER"
+ " (ID, KEYBOARD_POSITION, SORT_POSITION, UNICODE_CHAR, PRODUCT_FAMILY)"
+ " values(?, ?, ?, ?, ?)");
statement.setLong(1, character.getId());
statement.setInt(2, character.getKeyboardPosition());
statement.setInt(3, character.getSortPosition());
statement.setString(4, String.valueOf(character.getUnicodeChar()));
statement.setString(5, alphabet.getProductFamily().name());
statement.execute();
Java从中生成以下SQL查询:
insert into DLM.ALPHABET_CHARACTER (ID, KEYBOARD_POSITION, SORT_POSITION, UNICODE_CHAR, PRODUCT_FAMILY)
values(0, 0, 0, ' ','PRODUCTA');
对于较高的Ascii字符,例如鼾,变成:
insert into DLM.ALPHABET_CHARACTER (ID, KEYBOARD_POSITION, SORT_POSITION, UNICODE_CHAR, PRODUCT_FAMILY)
values(0, 0, 0, STRINGDECODE('\u9f3e'),'PRODUCTA');
编辑:
我发现了以下内容:
我不知道如何解决这个问题。
实体代码:
@Entity
@Cacheable
@Table(name = "alphabet_character")
public class AlphabetCharacterEntity {
@Id
@Column(updatable = false)
@GeneratedValue(generator = "localCounter")
@GenericGenerator(name = "localCounter",
strategy = "LocalCounterGenerator")
private Long id;
private Integer keyboardPosition;
private Integer sortPosition;
private Character unicodeChar;
@Enumerated(EnumType.STRING)
private ProductFamily productFamily;
}
JPA正在为此实体生成以下SQL数据库:
create table dlm.alphabet_character (id bigint not null, keyboard_position integer,
product_family varchar(255), sort_position integer, unicode_char char(255),
primary key (id));