Hibernate 预期内存中HSQL数据库将引发java.sql.SQLException值为大异常
我正在使用hibernate作为我的项目的JPA提供者。在生产环境中,我的带有hibernate的Java应用程序是针对11g Oracle DB运行的。在生产中Hibernate 预期内存中HSQL数据库将引发java.sql.SQLException值为大异常,hibernate,jpa,hsqldb,dump,pojo,Hibernate,Jpa,Hsqldb,Dump,Pojo,我正在使用hibernate作为我的项目的JPA提供者。在生产环境中,我的带有hibernate的Java应用程序是针对11g Oracle DB运行的。在生产中 <property name="hibernate.hbm2ddl.auto">create</property> 程序输出: DB转储: HSQL数据库版本: hsqldb hsqldb 1.8.0.10 设置了Dawid Pytel sql.enforce\u strict\u大小的可能重复
<property name="hibernate.hbm2ddl.auto">create</property>
程序输出:
DB转储:
HSQL数据库版本:
hsqldb
hsqldb
1.8.0.10
设置了Dawid Pytel sql.enforce\u strict\u大小的可能重复项。您还可以在HSQLDB中看到,该列是使用此限制创建的。对不起,我错过了。您是否尝试过将此参数作为连接参数传递,例如:jdbc:hsqldb:mem:。;sql.enforce_strict_size=true
我尝试了一个,它采用了配置,但我仍然可以填写一千个可能重复的Dawid Pytel sql.enforce_strict_size设置。您还可以在HSQLDB中看到,该列是使用此限制创建的。对不起,我错过了。您是否尝试过将此参数作为连接参数传递,例如:jdbc:hsqldb:mem:。;sql.enforce\u strict\u size=true
我尝试了那一个,它采用了配置,但我仍然可以填写1000个A
<property name="hibernate.hbm2ddl.auto"> create</property>
<property name="sql.enforce_strict_size">true</property>
public abstract class AbstractTestDao {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
@BeforeClass
public static void setUpBeforeClass() {
//Start HSQL database
HsqlServerUtil.getInstance().start("mem:testdb");
}
@AfterClass
public static void teardownClass() {
//End HSQL database
HsqlServerUtil.getInstance().stop();
}
void setUpOperation() {
//Set up test.hibernate.cfg.xml
HibernateUtil.setSessionFactory(configureSessionFactory());
}
/**
* Generic method for persisting entities into database.
*
* @param entity Java object from a (hibernate) class annotated with @Entity
*/
<T> void persistEntity(T entity) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.persist(entity);
session.beginTransaction().commit();
session.close();
}
/**
* Generic method for retrieving entities from database.
*
* @param cls Object class
* @param id Entity unique identifier (Primary Key)
* @return Casted Entity object from db
*/
<T> T getEntityById(Class<T> cls, Long id) {
Session session = HibernateUtil.getSessionFactory().openSession();
T entity = cls.cast(session.load(cls, id));
session.close();
return entity;
}
/**
* Don't forget to close your connection so that the in-mem db can release it resources.
*
* @return Connection object to the in-memory db HyperSQL.
* @throws SQLException
*/
public static Connection getNewConn() throws SQLException {
return DriverManager.getConnection("jdbc:hsqldb:mem:testdb;shutdown=false", "SA", "");
}
/**
* Makes a script from the current in-memory database and place a txt file in a directory.
* The script describes the current in-mem db including constrains.
*
* @param testName A String representing the junit test name used for describing the file.
* @throws SQLException
*/
void createDatabaseScript(final String testName) throws SQLException {
final String filePath = new File("").getAbsolutePath();
final String targetDir = "/target/Temp/Databasedump/";
final String directoryPath = filePath.concat(targetDir);
final File directory = new File(directoryPath);
if (!directory.exists()) {
directory.mkdir();
}
final String fileName = uniqueFileNameGenerator(directoryPath, testName);
final Connection conn = getNewConn();
Statement st = null;
ResultSet rs = null;
try {
st = conn.createStatement();
//Example: st.executeQuery("SCRIPT '../Temp/dump4.txt'")
final String sqlStatement = String.format("SCRIPT '%s%s'", directoryPath, fileName);
rs = st.executeQuery(sqlStatement);
} finally {
if (st != null) {
st.close();
}
if (rs != null) {
rs.close();
}
if (conn != null) {
conn.close();
}
}
}
/**
* Search for a unique filename in a directory with 1 to 2147483647 (Integer.MAX_VALUE) possibilities per date.
* May throw a StackOverflowError if all 2147483646 file names are given away.
*
* @param directoryPath - String representing the directory you want to use.
* @param name - String representing the filename you want to use.
* @return String representing a unique filename
*/
private String uniqueFileNameGenerator(final String directoryPath, final String name) {
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
final String todayDate = dateFormat.format(Calendar.getInstance().getTime());
final Integer randomNumber = (int) (Math.random() * Integer.MAX_VALUE + 1);
final String fileName = name + "_" + todayDate + "_" + randomNumber + ".txt";
File file = new File(directoryPath + fileName);
if (file.exists()) {
return uniqueFileNameGenerator(directoryPath, name);
}
return fileName;
}
}
<br>
<strong>BAG POJO:</strong>
</br>
@Entity
@Table(name = "BAG")
public class Bag implements Serializable {
public static final int FIELD_LENGTH_REMARKS = 128;
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "BAG_ID_GENERATOR", sequenceName = "BAG_ID_SEQ", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "BAG_ID_GENERATOR")
private Long id;
@Column(name = "REMARKS", length = FIELD_LENGTH_REMARKS)
private String remarks;
public Long getId() {
return id;
}
public void setId(final Long id) {
this.id = id;
}
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
}
public class BagDaoTest extends AbstractTestDao {
@Before
public void setup() {
//Setup HSQL tables every JUnit test
setUpOperation();
}
@Test(expected=SQLException.class)
public void remarkBagValueExceeded() throws SQLException {
Bag bag = new Bag();
final char[] array = new char[1000];
Arrays.fill(array, 'A');
bag.setRemarks(String.valueOf(array));
persistEntity(bag);
//Expecting a java.sql.SQLException value to large exception here.
Bag retrieved = getEntityById(Bag.class, bag.getId());
Assert.assertNotNull(retrieved);
System.out.println("Id was: " + retrieved.getId());
System.out.println("Remarks is: " + retrieved.getRemarks());
try {
createDatabaseScript("remarkBagValueExceeded");
}catch (SQLException sqe) {
System.out.println("Sql exception was thrown when trying to dump the DB");
}
}
}
Id was: 1
Remarks is: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.. //many more A
CREATE SCHEMA PUBLIC AUTHORIZATION DBA
CREATE SEQUENCE BAG_ID_SEQ AS INTEGER START WITH 1
CREATE MEMORY TABLE BAG(ID BIGINT NOT NULL PRIMARY KEY, REMARKS VARCHAR(128))
CREATE USER SA PASSWORD ""
GRANT DBA TO SA
SET SCHEMA PUBLIC
INSERT INTO COM_SEALBAG VALUES(1,'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA..//many more A')
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.10</version>
</dependency>