时间:2015-04-28 08:31:55

标签: oracle hibernate jpa

我有2个实体(这只是一个简化的例子,而不是真实的实体):

@Entity
@Table(name = "entityA")
public class EntityA {
    @ManyToOne(cascade = CascadeType.ALL)
    EntityB start;
    @ManyToOne(cascade = CascadeType.ALL)
    EntityB stop;
    ...
}


@Entity
@Table(name = "entityB")
public class EntityB {
    public String name; 
    ...
}

当我尝试使用以下命令编写查询时,Hibernate崩溃:

select t.start from EntityA t
where 
...
order by t.start.name

它不允许我放入t.start.name,如果我用t.start替换它,它完美地工作。 有谁知道它为什么会这样?

修改 这是由hibernate翻译的查询:

SELECT DISTINCT entityB1_.uuid AS uuid2021_, ..
  entityB1_.name               AS name2021_,
...
FROM entityA entityA0_
INNER JOIN entityB entityB1_
ON entityA0_.stop_uuid=entityB1_.uuid,
  entityB entityB2_,
  entityB entityB3_
WHERE entityA0_.start_uuid    =entityB2_.uuid
AND entityA0_.start_uuid=entityB3_.uuid
AND entityA0_.qn        =?
AND entityB2_.lifeCycle            ='READY'
AND entityB3_.lifeCycle            ='READY'
ORDER BY entityB2_.name

正如你在顺序中所看到的那样,放置" entityB2"而不是" entityB1",这是选定的字段。 通过这种方式,它给了我这个例外:

  

引起:java.sql.SQLSyntaxErrorException:ORA-01791:不是SELECTed   表达

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
     

〜[ojdbc7.jar:12.1.0.1.0]           at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399)   〜[ojdbc7.jar:12.1.0.1.0]           at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1017)   〜[ojdbc7.jar:12.1.0.1.0]           在oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:655)〜[ojdbc7.jar:12.1.0.1.0]           在oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:249)〜[ojdbc7.jar:12.1.0.1.0]           在oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:566)〜[ojdbc7.jar:12.1.0.1.0]           at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:215)   〜[ojdbc7.jar:12.1.0.1.0]           at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:58)   〜[ojdbc7.jar:12.1.0.1.0]           at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:776)   〜[ojdbc7.jar:12.1.0.1.0]           at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:897)   〜[ojdbc7.jar:12.1.0.1.0]           at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1034)   〜[ojdbc7.jar:12.1.0.1.0]           at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3820)   〜[ojdbc7.jar:12.1.0.1.0]           at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3867)   〜[ojdbc7.jar:12.1.0.1.0]           at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1502)   〜[ojdbc7.jar:12.1.0.1.0]           在weblogic.jdbc.wrapper.PreparedStatement.executeQuery(PreparedStatement.java:141)   〜[weblogic.server.merged.jar:12.1.3.0.0]           在org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)   〜[冬眠核-3.5.6-Final.jar:3.5.6决赛]           在org.hibernate.loader.Loader.getResultSet(Loader.java:1869)〜[hibernate-core-3.5.6-Final.jar:3.5.6-Final]           在org.hibernate.loader.Loader.doQuery(Loader.java:718)〜[hibernate-core-3.5.6-Final.jar:3.5.6-Final]           在org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)   〜[冬眠核-3.5.6-Final.jar:3.5.6决赛]           在org.hibernate.loader.Loader.doList(Loader.java:2449)〜[hibernate-core-3.5.6-Final.jar:3.5.6-Final]

1 个答案:

答案 0 :(得分:1)

尝试这样的事情,看看这是否会阻止Hibernate不必要地添加联接

create procedure update_tick_user
@UserId         varchar(32),
@ClientId       varchar(32),
@Table          varchar(64),
@TableRecord    varchar(512),
@Descr          varchar(128),
@RemoteIP       varchar(16)
as
begin
    insert into tick_audit (user_account, client_id, date_time, table_name, table_record_id, descr, remote_ip_address)
    values
    (@UserId, @ClientId, getdate(), @Table, @TableRecord, @Descr, @RemoteIP)
end;