Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数据初始化问题-使用hibernate.hbm2ddl.auto=create-hibernate JPA复制密钥条目_Hibernate_Jpa - Fatal编程技术网

数据初始化问题-使用hibernate.hbm2ddl.auto=create-hibernate JPA复制密钥条目

数据初始化问题-使用hibernate.hbm2ddl.auto=create-hibernate JPA复制密钥条目,hibernate,jpa,Hibernate,Jpa,据我所知,我刚刚在启动Singleton中发现了在后期构造期间数据初始化的另一个问题。该问题是由主键冲突引起的,主键冲突是在以下过程中生成的: @Singleton @Startup public class DefaultDataIntializer { @PostConstruct public void init() { BlinkUser bu = createDefaultUser(); BlinkGroup bg = createDefaultGroup

据我所知,我刚刚在启动Singleton中发现了在后期构造期间数据初始化的另一个问题。该问题是由主键冲突引起的,主键冲突是在以下过程中生成的:

@Singleton
@Startup
public class DefaultDataIntializer {

  @PostConstruct
  public void init()
  {
    BlinkUser bu = createDefaultUser();
    BlinkGroup bg = createDefaultGroup();
    Link l = createDefaultLink();
    UserLink ul = createDefaultUserLink();

    if(buf.find(bu.getEmail()) != null)
      return;

    bg.addUser(bu);
    ul.setLink(l);
    ul.setOwner(bu);

    lf.create(l);
    bgf.create(bg);
    buf.create(bu);
    ulf.create(ul);     
  } 
}
并且是完整部署的日志。问题在于:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'user@gmail.com' for key 'PRIMARY'
但当我使用调试器时,这个问题在创建过程中不会出现。可能是缓存造成的?好吧,这是不可能的,我只是没有发现任何错误,特别是在创建DefaultDataInitializer::init:5之前检查示例数据的存在性。另外,它是my persistence.xml:

 <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
   <persistence-unit name="BlinkLinkServer-ejbPU" transaction-type="JTA">
   <provider>org.hibernate.ejb.HibernatePersistence</provider>
   <jta-data-source>java:jboss/datasources/BlinkLinkDataSource</jta-data-source>
   <exclude-unlisted-classes>false</exclude-unlisted-classes>
   <properties>
     <property name="hibernate.hbm2ddl.auto" value="create"/>
     <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
   </properties>
 </persistence-unit>
}

其中:

 16:08:21,079 WARNING [null] (ServerService Thread Pool -- 424) Initializing data!778044534
 16:08:21,116 WARNING [null] (ServerService Thread Pool -- 423) Initializing data!1060447455
好的,所以这个问题的真正原因是项目中重复的ejb JAR——一个在战争中,另一个打包在EAR中。我不知道调试器怎么会不让我第二次调用@PostConstruct方法。谢谢你的帮助,这不是我一个人解决的

解决方案: 检查Maven项目。WAR中的pom.xml应具有相关性:

    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>YourProject-ejb</artifactId>
        <version>${project.version}</version>
    </dependency>
那么jar就不会被纳入战争

将skinnyWar选项添加到maven war插件配置中

出于某种原因,您可能希望将JAR文件保存在WAR中并从EAR中删除。所以您可以简单地将提供的选项添加到EAR文件中的依赖项中

<dependency>
    <groupId>com.example.project</groupId>
    <artifactId>YourProject-ejb</artifactId>
    <version>1.0-SNAPSHOT</version>
    <type>ejb</type>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>com.example.project</groupId>
    <artifactId>YourProject-web</artifactId>
    <version>1.0-SNAPSHOT</version>
    <type>war</type>
</dependency>

你能检查一下用户的电话号码吗user@gmail.com'是否存在于您的数据库中?可能错误在buf.find中。它找不到用户,但它存在于数据库中。是的,它已经存在了。但即使是create方法也不会引起任何异常。这看起来像是选择中的某个事务?未正确更新。您创建默认对象,然后检查电子邮件是否已存在并返回,但即使您返回,事务也会被提交,并希望写入新数据。我必须先用getDefaultEmail之类的东西进行检查。抛出异常会回滚事务,但我认为这会阻止应用程序启动。事实并非如此,因为createDefaultUser和其他方法调用简单构造函数,所以这些对象尚未持久化。您是否在实体模型BlinkUser、BlinkGroup等中实现了此操作,。。。hashCode和equals方法?这也可能是问题所在。
    <dependency>
        <groupId>com.example.project</groupId>
        <artifactId>YourProject-ejb</artifactId>
        <version>1.0-SNAPSHOT</version>
        <type>ejb</type>
    </dependency>
    <dependency>
        <groupId>com.example.project</groupId>
        <artifactId>YourProject-web</artifactId>
        <version>1.0-SNAPSHOT</version>
        <type>war</type>
    </dependency>
<dependency>
    <groupId>${project.groupId}</groupId>
    <artifactId>YourProject-ejb</artifactId>
    <version>${project.version}</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>com.example.project</groupId>
    <artifactId>YourProject-ejb</artifactId>
    <version>1.0-SNAPSHOT</version>
    <type>ejb</type>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>com.example.project</groupId>
    <artifactId>YourProject-web</artifactId>
    <version>1.0-SNAPSHOT</version>
    <type>war</type>
</dependency>