2013-08-06 43 views
1

我正在使用hibernate和oracle數據庫嘗試使用序列將自動ID插入到表中。序列明確存在於數據庫中,但hibernate似乎無法找到它。休眠/ Oracle序列不起作用

這裏是所有相關信息:

 
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". 
SLF4J: Defaulting to no-operation (NOP) logger implementation 
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. 
org.hibernate.exception.SQLGrammarException: could not get next sequence value 
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92) 
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) 
     .... 
Caused by: java.sql.SQLSyntaxErrorException: ORA-02289: sequence does not exist 
     .... 
    ... 12 more 

我知道它說:「產生的原因:java.sql.SQLSyntaxErrorException:ORA-02289:序列不存在」,但我可以對數據庫訪問的序列:

表:

CREATE TABLE Property(
    id INT, 
    address VARCHAR2(50), 
    town VARCHAR2(50), 
    postCode VARCHAR2(50), 
    purchasePrice INT 
); 

序列:

create sequence property_seq start with 1 increment by 1 nomaxvalue; 

XML映射:

<class name="com.rental.model.property.Property" table="PROPERTY"> 
    <meta attribute="class-description"> This class contains the property detail. </meta> 
    <id name="id" type="integer" column="id"> 
     <generator class="sequence"/> 
    </id> 
    <property name="address" column="ADDRESS" type="string" /> 
    <property name="town" column="TOWN" type="string" /> 
    <property name="postCode" column="POSTCODE" type="string" /> 
    <property name="purchasePrice" column="PURCHASEPRICE" type="integer" /> 
</class> 

註釋:

@Id 
@SequenceGenerator(name="property_seq", sequenceName="property_seq", allocationSize=1, initialValue=1) 
@GeneratedValue (strategy = GenerationType.SEQUENCE, generator="property_seq") 
public int getId() { 
    return id; 
} 
+0

序列對象是否在與登錄到休眠會話的用戶相同的模式中創建?如果沒有,您是否創建了同義詞並授予序列的訪問權限? – OldProgrammer

+0

也許嘗試在property_seq上添加公共同義詞 – tbone

+0

Hibernate使用的用戶與創建序列的用戶相同 – Paul

回答

2

你爲什麼要使用XML和@Annotation在同一時間?也許xml定義勝過註釋,而Hibernate正在回顧默認序列,而不是您的property_seq
嘗試刪除xml映射並檢查它是否有效。

+0

感謝Bellabax。我完全刪除XML和其他所有添加註釋,然後改變了我的工廠。 \t \t \t廠=新AnnotationConfiguration()配置()addAnnotatedClass(Property.class)。 buildSessionFactory(); – Paul

+0

我很高興你解決了! –