找不到JPA父键

时间:2015-08-11 10:27:43

标签: java spring jpa

我正在使用Spring Mvc和JPA,实际上我在创建数据库之前做了实体(我现在将数据存储在内存中),但是当我尝试启动应用程序时,它会抛出:

ago 11, 2015 11:52:57 AM org.apache.catalina.startup.Catalina load
INFORMACIÓN: Initialization processed in 826 ms
2015-08-11 11:53:01.357 [localhost-startStop-1] ERROR o.h.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table accessMenu drop constraint FK_9r7dom4l6r6u2upfxc16fbdll
2015-08-11 11:53:01.359 [localhost-startStop-1] ERROR o.h.tool.hbm2ddl.SchemaExport - usuario no tiene privilegios suficientes o objeto no encontrado: PUBLIC.ACCESSMENU
2015-08-11 11:53:01.359 [localhost-startStop-1] ERROR o.h.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table accessMenu drop constraint FK_3d5qup2fkqsb5y07o6qkrl4jn
2015-08-11 11:53:01.359 [localhost-startStop-1] ERROR o.h.tool.hbm2ddl.SchemaExport - usuario no tiene privilegios suficientes o objeto no encontrado: PUBLIC.ACCESSMENU
2015-08-11 11:53:01.359 [localhost-startStop-1] ERROR o.h.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table accessMenu drop constraint FK_5grkivrkck7b50gjv62culvgu
2015-08-11 11:53:01.359 [localhost-startStop-1] ERROR o.h.tool.hbm2ddl.SchemaExport - usuario no tiene privilegios suficientes o objeto no encontrado: PUBLIC.ACCESSMENU
2015-08-11 11:53:01.359 [localhost-startStop-1] ERROR o.h.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table accessMenu drop constraint FK_5grkivrkck7b50gjv62culvgu
2015-08-11 11:53:01.359 [localhost-startStop-1] ERROR o.h.tool.hbm2ddl.SchemaExport - usuario no tiene privilegios suficientes o objeto no encontrado: PUBLIC.ACCESSMENU
2015-08-11 11:53:01.359 [localhost-startStop-1] ERROR o.h.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table menu drop constraint FK_dqqalr782gcf0yqu0ej1tvaky
2015-08-11 11:53:01.359 [localhost-startStop-1] ERROR o.h.tool.hbm2ddl.SchemaExport - usuario no tiene privilegios suficientes o objeto no encontrado: PUBLIC.MENU
2015-08-11 11:53:01.360 [localhost-startStop-1] ERROR o.h.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table user drop constraint FK_1mcjtpxmwom9h9bf2q0k412e0
2015-08-11 11:53:01.360 [localhost-startStop-1] ERROR o.h.tool.hbm2ddl.SchemaExport - usuario no tiene privilegios suficientes o objeto no encontrado: PUBLIC.USER
2015-08-11 11:53:01.360 [localhost-startStop-1] ERROR o.h.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table user drop constraint FK_8qtpnv06elxuryeuv1ac4ximm
2015-08-11 11:53:01.360 [localhost-startStop-1] ERROR o.h.tool.hbm2ddl.SchemaExport - usuario no tiene privilegios suficientes o objeto no encontrado: PUBLIC.USER
2015-08-11 11:53:01.364 [localhost-startStop-1] ERROR o.h.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table accessMenu add constraint FK_5grkivrkck7b50gjv62culvgu foreign key (id) references menu
2015-08-11 11:53:01.365 [localhost-startStop-1] ERROR o.h.tool.hbm2ddl.SchemaExport - nombre del objeto ya existe: FK_5GRKIVRKCK7B50GJV62CULVGU in statement [alter table accessMenu add constraint FK_5grkivrkck7b50gjv62culvgu foreign key (id) references menu]
2015-08-11 11:53:01.755 [localhost-startStop-1] DEBUG e.d.c.FilterInvocationSecurityMetadataSourcePostProcessor - Url Roles object :{}
2015-08-11 11:53:01.924 [localhost-startStop-1] WARN  o.h.e.j.i.JdbcCoordinatorImpl - HHH000386: ResultSet had no statement associated with it, but was not yet registered
2015-08-11 11:53:01.950 [localhost-startStop-1] WARN  o.h.e.j.i.JdbcCoordinatorImpl - HHH000386: ResultSet had no statement associated with it, but was not yet registered
2015-08-11 11:53:01.956 [localhost-startStop-1] WARN  o.h.e.j.i.JdbcCoordinatorImpl - HHH000386: ResultSet had no statement associated with it, but was not yet registered
2015-08-11 11:53:01.961 [localhost-startStop-1] WARN  o.h.e.j.i.JdbcCoordinatorImpl - HHH000386: ResultSet had no statement associated with it, but was not yet registered
2015-08-11 11:53:01.968 [localhost-startStop-1] WARN  o.h.e.j.i.JdbcCoordinatorImpl - HHH000386: ResultSet had no statement associated with it, but was not yet registered
2015-08-11 11:53:01.973 [localhost-startStop-1] WARN  o.h.e.j.i.JdbcCoordinatorImpl - HHH000386: ResultSet had no statement associated with it, but was not yet registered
2015-08-11 11:53:01.978 [localhost-startStop-1] WARN  o.h.e.j.i.JdbcCoordinatorImpl - HHH000386: ResultSet had no statement associated with it, but was not yet registered
2015-08-11 11:53:01.979 [localhost-startStop-1] WARN  o.h.e.j.i.JdbcCoordinatorImpl - HHH000386: ResultSet had no statement associated with it, but was not yet registered
2015-08-11 11:53:01.982 [localhost-startStop-1] WARN  o.h.e.j.i.JdbcCoordinatorImpl - HHH000386: ResultSet had no statement associated with it, but was not yet registered
2015-08-11 11:53:01.984 [localhost-startStop-1] WARN  o.h.e.j.i.JdbcCoordinatorImpl - HHH000386: ResultSet had no statement associated with it, but was not yet registered
2015-08-11 11:53:02.006 [localhost-startStop-1] WARN  o.h.e.j.i.JdbcCoordinatorImpl - HHH000386: ResultSet had no statement associated with it, but was not yet registered
2015-08-11 11:53:02.010 [localhost-startStop-1] WARN  o.h.e.j.i.JdbcCoordinatorImpl - HHH000386: ResultSet had no statement associated with it, but was not yet registered
2015-08-11 11:53:02.015 [localhost-startStop-1] WARN  o.h.e.j.i.JdbcCoordinatorImpl - HHH000386: ResultSet had no statement associated with it, but was not yet registered
2015-08-11 11:53:02.019 [localhost-startStop-1] WARN  o.h.e.j.i.JdbcCoordinatorImpl - HHH000386: ResultSet had no statement associated with it, but was not yet registered
2015-08-11 11:53:02.024 [localhost-startStop-1] WARN  o.h.e.j.spi.SqlExceptionHelper - SQL Error: -177, SQLState: 23503
2015-08-11 11:53:02.024 [localhost-startStop-1] ERROR o.h.e.j.spi.SqlExceptionHelper - violación del restricción de integridad: no ha registro padre en clave foránea; FK_5GRKIVRKCK7B50GJV62CULVGU table: ACCESSMENU
[...]
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:74) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:136) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2975) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3487) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:214) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:194) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:178) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:321) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:286) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:192) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.ejb.event.EJB3MergeEventListener.saveWithGeneratedId(EJB3MergeEventListener.java:71) ~[hibernate-entitymanager-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.event.internal.DefaultMergeEventListener.saveTransientEntity(DefaultMergeEventListener.java:236) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:216) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:154) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:76) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:914) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:898) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:902) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:889) ~[hibernate-entitymanager-4.2.3.Final.jar:4.2.3.Final]
    ... 66 common frames omitted
Caused by: java.sql.SQLIntegrityConstraintViolationException: violación del restricción de integridad: no ha registro padre en clave foránea; FK_5GRKIVRKCK7B50GJV62CULVGU table: ACCESSMENU
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source) ~[hsqldb-2.3.0.jar:2.3.0]
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source) ~[hsqldb-2.3.0.jar:2.3.0]
    at org.hsqldb.jdbc.JDBCPreparedStatement.fetchResult(Unknown Source) ~[hsqldb-2.3.0.jar:2.3.0]
    at org.hsqldb.jdbc.JDBCPreparedStatement.executeUpdate(Unknown Source) ~[hsqldb-2.3.0.jar:2.3.0]
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:133) ~[hibernate-core-4.2.3.Final.jar:4.2.3.Final]
    ... 88 common frames omitted
Caused by: org.hsqldb.HsqlException: violación del restricción de integridad: no ha registro padre en clave foránea; FK_5GRKIVRKCK7B50GJV62CULVGU table: ACCESSMENU
    at org.hsqldb.error.Error.error(Unknown Source) ~[hsqldb-2.3.0.jar:2.3.0]
    at org.hsqldb.Constraint.getException(Unknown Source) ~[hsqldb-2.3.0.jar:2.3.0]
    at org.hsqldb.Constraint.checkInsert(Unknown Source) ~[hsqldb-2.3.0.jar:2.3.0]
    at org.hsqldb.StatementDML.performIntegrityChecks(Unknown Source) ~[hsqldb-2.3.0.jar:2.3.0]
    at org.hsqldb.StatementDML.insertRowSet(Unknown Source) ~[hsqldb-2.3.0.jar:2.3.0]
    at org.hsqldb.StatementInsert.getResult(Unknown Source) ~[hsqldb-2.3.0.jar:2.3.0]
    at org.hsqldb.StatementDMQL.execute(Unknown Source) ~[hsqldb-2.3.0.jar:2.3.0]
    at org.hsqldb.Session.executeCompiledStatement(Unknown Source) ~[hsqldb-2.3.0.jar:2.3.0]
    at org.hsqldb.Session.execute(Unknown Source) ~[hsqldb-2.3.0.jar:2.3.0]
    ... 91 common frames omitted
ago 11, 2015 11:53:02 AM org.apache.catalina.startup.Catalina start
INFORMACIÓN: Server startup in 4191 ms

Menu.java

@Entity
@Table(name = "menu")
@NamedQuery(name = Menu.FIND_BY_URL, query = "select a from Menu a where a.url = :url")
public class Menu { 

    public static final String FIND_BY_URL = "Menu.findByUrl";

    @Id
    @GeneratedValue
    private Long id;

    private String nombre;

    private String url;

    @ManyToOne(cascade=CascadeType.ALL,targetEntity = Menu.class, fetch=FetchType.LAZY)
    @JoinColumn(name="parentMenu", referencedColumnName="id")
    private Menu parentMenu;

    @OneToMany(mappedBy="id", cascade=CascadeType.ALL)
    private Set<AccessMenu> accessMenu = new HashSet<AccessMenu>();

    @OneToMany(mappedBy="parentMenu",fetch=FetchType.LAZY,cascade=CascadeType.ALL)
    private Set<Menu> subMenus = new HashSet<Menu>();

    [geters and setters...]

AccessMenu.java

@Entity
@Table(name = "accessMenu")
@NamedQuery(name = AccessMenu.FIND_BY_URL, query = "select a from AccessMenu a where a.menu.url = :url")
public class AccessMenu {

    public static final String FIND_BY_URL = "AccessMenu.findByUrl";

    @Id
    @GeneratedValue
    private Long id;

    private Boolean access;

    @ManyToOne(cascade=CascadeType.ALL,targetEntity = Profile.class)
    @JoinColumn(name="profile", referencedColumnName="id")
    private Profile profile;

    @ManyToOne(cascade=CascadeType.ALL,targetEntity = Menu.class)
    @JoinColumn(name="menu", referencedColumnName="id")
    private Menu menu;

[getters and setters...]

应用程序崩溃的init方法首先设置Menu

private void obtieneMenus(MenuRepository menuRepo) {
        Menu menuBarra = new Menu();
        menuBarra.setNombre("inicio");
        menuBarra.setUrl("/");
        menuRepo.save(menuBarra);
        Menu menuRegistro = new Menu();
        menuRegistro.setNombre("signup");
        menuRegistro.setUrl("/signup");
        menuRegistro.setParentMenu(menuRepo.findByUrl(menuBarra.getUrl()));
        menuRepo.save(menuRegistro);
        Menu menuLogin = new Menu();
        menuLogin.setNombre("signin");
        menuLogin.setUrl("/signin");
        menuLogin.setParentMenu(menuRepo.findByUrl(menuBarra.getUrl()));
        menuRepo.save(menuLogin);
        Menu menuPerson = new Menu();
        menuPerson.setNombre("person");
        menuPerson.setUrl("/person");
        menuPerson.setParentMenu(menuRepo.findByUrl(menuBarra.getUrl()));
        menuRepo.save(menuPerson);
        Menu menuCreate = new Menu();
        menuCreate.setNombre("personCreate");
        menuCreate.setUrl("/person/create");
        menuCreate.setParentMenu(menuRepo.findByUrl(menuPerson.getUrl()));
        menuRepo.save(menuCreate);
        Menu menuListado = new Menu();
        menuListado.setNombre("personList");
        menuListado.setUrl("/person/list");
        menuListado.setParentMenu(menuRepo.findByUrl(menuPerson.getUrl()));
        menuRepo.save(menuListado);

然后我初始化Profile(但我认为这不是问题)并最终设置AccessMenu

private List<AccessMenu> obtieneAccesos(List<Menu> menus, List<Profile> perfiles){
        List<AccessMenu> permisos = new ArrayList<AccessMenu>();
        AccessMenu acceso;
        for(Menu menu:menus){
            acceso = new AccessMenu();
            acceso.setMenu(menu);
            for(Profile perfil : perfiles){
                acceso.setProfile(perfil);
                if("/person/list".equals(menu.getUrl()) && "ROLE_p1".equals(perfil.getNombre())){
                    acceso.setAccess(false);
                }else{
                    acceso.setAccess(true);
                }
                permisos.add(acceso);
            }
        }
        return permisos;
    }

现在,我不知道问题出在初始化方法还是JPA标签中......

1 个答案:

答案 0 :(得分:1)

尝试从Menu.java的mappedBy属性中的OneToMany标记更改accesMenu属性。

@OneToMany(mappedBy="menu")
    private Set<AccessMenu> accessMenu = new HashSet<AccessMenu>();