首页 \ 问答 \ Hibernate不能生成id(Hibernate can't generate id)

Hibernate不能生成id(Hibernate can't generate id)

我在我的IntelliJ IDEA上创建了一个新项目,使用hibernate(+ hibernate entitymanager)5,hsqldb 1.8(仅用于测试)以及junit 4。

我有我的测试课

UserRoundRelationsTest

EntityManagerFactory emf;
EntityManager em;

Date currentDate;
User user;
Round round1;
Round round2;

还有一些@After和@Before方法:

@Before
public void saveDummyUserAndTwoDummyRounds() {

    emf = Persistence.createEntityManagerFactory("testPersistence");
    em = emf.createEntityManager(); // Retrieve an application managed entity manager

    currentDate = new Date();
    user = new User.UserBuilder("alias").fullName("Full Name").phoneNumber("Phone Number").height(100).weight(100).birthDate(currentDate).build();

    round1 = new Round(new GameMode(1, ButtonTargetStrategy.RANDOM, ButtonSkipStrategy.SKIP_ON_MISTAKE));
    round2 = new Round(new GameMode(2, ButtonTargetStrategy.RANDOM, ButtonSkipStrategy.SKIP_ON_MISTAKE));

    round1.setUser(user);
    round2.setUser(user);

    HashSet<Round> rounds = new HashSet<Round>(2);
    rounds.add(round1);
    rounds.add(round2);

    em.getTransaction().begin();

    user.setRounds(rounds);

    em.persist(user);
    em.persist(round1);
    em.persist(round2);

    em.flush();
    em.getTransaction().commit();
}

@After
public void deleteData() {

    em.remove(user);
    em.remove(round2);
    em.remove(round2);

    em.getTransaction().commit();

    em.close();
    emf.close();
}

首先,当我尝试运行测试时,hibernate无法找到User和Round类的表以及它们依赖的所有内容(例如,称为ButtonPress的类)。 我认为这是由于我在persistence.xml中配置的自动创建 - 删除

<persistence-unit name="testPersistence">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

    <class>com.carloworks.model.User</class>
    <class>com.carloworks.model.Round</class>
    <class>com.carloworks.model.ButtonPress</class>
    <properties>
        <!--<property name="hibernate.archive.autodetection" value="class,hbm"></property>-->

        <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"></property>
        <property name="hibernate.show_sql" value="true"></property>
        <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"></property>
        <property name="hibernate.connection.username" value="sa"></property>
        <property name="hibernate.connection.password" value=""></property>
        <property name="hibernate.connection.url"
                  value="jdbc:hsqldb:mem:buttonMasherModelsTestDb;MV_STORE=FALSE;MVCC=FALSE"></property>
        <!--<property name="hibernate.default_schema" value="buttonMasherModelsTestDb"/>-->
        <property name="hibernate.hbm2ddl.auto" value="create-drop"></property>
    </properties>
</persistence-unit>

进一步向下滚动错误日志,我也在这一行发生错误:

em.persist(user);

空标识符(加'不支持此函数'/'GenericJDBCException:无法准备语句')。

我做错了什么?

该项目也在Github上提供

完整的错误日志


I've created a new project on my IntelliJ IDEA, using hibernate (+ hibernate entitymanager) 5, hsqldb 1.8 (for testing only), as well as junit 4.

I've got my test class

UserRoundRelationsTest

EntityManagerFactory emf;
EntityManager em;

Date currentDate;
User user;
Round round1;
Round round2;

And some @After and @Before methods:

@Before
public void saveDummyUserAndTwoDummyRounds() {

    emf = Persistence.createEntityManagerFactory("testPersistence");
    em = emf.createEntityManager(); // Retrieve an application managed entity manager

    currentDate = new Date();
    user = new User.UserBuilder("alias").fullName("Full Name").phoneNumber("Phone Number").height(100).weight(100).birthDate(currentDate).build();

    round1 = new Round(new GameMode(1, ButtonTargetStrategy.RANDOM, ButtonSkipStrategy.SKIP_ON_MISTAKE));
    round2 = new Round(new GameMode(2, ButtonTargetStrategy.RANDOM, ButtonSkipStrategy.SKIP_ON_MISTAKE));

    round1.setUser(user);
    round2.setUser(user);

    HashSet<Round> rounds = new HashSet<Round>(2);
    rounds.add(round1);
    rounds.add(round2);

    em.getTransaction().begin();

    user.setRounds(rounds);

    em.persist(user);
    em.persist(round1);
    em.persist(round2);

    em.flush();
    em.getTransaction().commit();
}

and

@After
public void deleteData() {

    em.remove(user);
    em.remove(round2);
    em.remove(round2);

    em.getTransaction().commit();

    em.close();
    emf.close();
}

Firstly, when I try to run the test, hibernate can't find the tables for User and Round classes, as well as everything they are dependent on (a class called ButtonPress for example). I assume this happens due to the automatic create-drop I have configured at my persistence.xml

<persistence-unit name="testPersistence">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

    <class>com.carloworks.model.User</class>
    <class>com.carloworks.model.Round</class>
    <class>com.carloworks.model.ButtonPress</class>
    <properties>
        <!--<property name="hibernate.archive.autodetection" value="class,hbm"></property>-->

        <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"></property>
        <property name="hibernate.show_sql" value="true"></property>
        <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"></property>
        <property name="hibernate.connection.username" value="sa"></property>
        <property name="hibernate.connection.password" value=""></property>
        <property name="hibernate.connection.url"
                  value="jdbc:hsqldb:mem:buttonMasherModelsTestDb;MV_STORE=FALSE;MVCC=FALSE"></property>
        <!--<property name="hibernate.default_schema" value="buttonMasherModelsTestDb"/>-->
        <property name="hibernate.hbm2ddl.auto" value="create-drop"></property>
    </properties>
</persistence-unit>

Scrolling further down the error logs, I also get an error at this line:

em.persist(user);

null identifier (plus 'This function is not supported'/'GenericJDBCException: could not prepare statement').

What did I do wrong?

The project is also available on Github

Full Error Log


原文:https://stackoverflow.com/questions/41063083
更新时间:2022-12-31 21:12

最满意答案

如果您使用某些符号通过某些#if预处理程序指令进行测试,那么该符号应该在预处理时定义(这在C ++编译器实际解析源文件之前发生)。

所以你可能想要使用

 #define HORIZONTAL_PIXELS 240

代替

 static const uint8_t HORIZONTAL_PIXELS = 240;

如果你真的需要这样一个const你可以用它来命名:

static const uint8_t k_HORIZONTAL_PIXELS = HORIZONTAL_PIXELS;

阅读有关C&C ++预处理器的更多信息。 如果你有一个源文件foo.cc尝试获取其预处理表单

g++ -C -E foo.cc > foo.ii

(也许添加其他预处理标志,-I ...或-D ....)然后用寻呼机或编辑器查看生成的foo.ii


if you use some symbol to be tested by some #if preprocessor directive, that symbol should be defined at preprocessing time (which happens before the actual parsing of your source file by the C++ compiler).

So you probably want to use

 #define HORIZONTAL_PIXELS 240

instead of

 static const uint8_t HORIZONTAL_PIXELS = 240;

If you really need such a const you could name it otherwise:

static const uint8_t k_HORIZONTAL_PIXELS = HORIZONTAL_PIXELS;

Read more about the C & C++ preprocessor. If you have a source file foo.cc try to get its preprocessed form with

g++ -C -E foo.cc > foo.ii

(perhaps adding other preprocessing flags like -I... or -D....) then look with a pager or an editor into the generated foo.ii

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。