2016-06-06 1 views
1

프로젝트에서 작업하는 동안 원래 Java 클래스에서 @Entity, @Table, @Column, @SequenceGenerator 및 @GeneratedValue Hibernate Annotations를 사용하여 항목을 성공적으로 추가 할 수있었습니다 내 오라클 데이터베이스에.Hibernate Annotations에서 hbm.xml 파일로 전환하는 중 오류가 발생했습니다.

이제 똑같은 내용을 복제하려고하는데 * .hbm.xml 파일을 사용하고 문제가 발생했습니다.

다음
//@Entity 
//@Table (name="client") 
@SequenceGenerator(name="seq_client",sequenceName="BIMB2013WMMEE.seq_client", 
allocationSize=1, initialValue=1) 
public class Client { 

    //Fields 
    //@Id 
    //@GeneratedValue(strategy=GenerationType.SEQUENCE) 
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq_client") 
    //@Column(name="CLIENT_ID") 
    private int id; 
    //@Column(name="CLIENT_NAME") 
    private String clientName; 
    //@Column(name="CLIENT_CODE") 
    private String clientCode; 

내 프로젝트의 src 디렉토리에있는 해당 hbm.xml 파일입니다 : 여기

는 주석 주석 원래 자바 클래스 코드입니다.

<hibernate-configuration> 

    <session-factory> 

     <!-- JDBC Database connection settings --> 
     <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 
     <property name="connection.url">jdbc:oracle:thin:@endeavour.us.manh.com:1523/pso11r2f</property> 
     <property name="connection.username">BIMB2013WMMEE</property> 
     <property name="connection.password">BIMB2013WMMEE</property> 

     <!-- JDBC connection pool settings ... using built-in test pool --> 
     <property name="connection.pool_size">1</property> 

     <!-- Select our SQL dialect --> 
     <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> 

     <!-- Echo the SQL to stdout --> 
     <property name="show_sql">true</property> 


     <!-- Set the current session context --> 
     <property name="current_session_context_class">thread</property> 

    </session-factory> 

</hibernate-configuration> 

마지막으로 여기 이클립스 오류 코드 :

Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.luv2code.hibernate.demo.entity.Client 

내가 실제로 객체를 생성하고 세션을 통해 데이터베이스에 추가되는 클래스를 변경하지 않았다 ... 해야합니까?

도움을 주셔서 감사합니다.

답변

0

당신이 보여 XML 파일은 최대 절전 모드 설정 파일이있다 h하지 않다. .xml 파일. 당신이 만든 각각의 영속 엔티티에 대해 "classname.hbm.xml"파일을 만들어야합니다. 귀하의 경우에는 그것이 Client 클래스입니다. Client.hbm.xml 파일을 만들어야합니다. 그런 다음 그 리소스를 설정 파일과 Hibernate 유틸리티 파일에 추가해야합니다. 도움이 될 것입니다. http://www.mkyong.com/hibernate/how-to-add-hibernate-xml-mapping-file-hbm-xml-programmatically/

+0

그래서 내가 생각하고있는 것은 factory = new Configuration(). configure ("hibernate.cfg.xml"). addAnnotatedClass ("Client.class") ...... to .configure (". .. "). addResource ("Client.hbm.xml) ..... 내가 할 때 새로운 오류 : "main"스레드의 예외 org.hibernate.MappingException : 엔터티 클래스를 찾을 수 없음 : 클라이언트 – AHijaouy

+0

그 리소스를 당신의 최대 절전 모드 설정 파일에 추가 했습니까? 튜토리얼에 표시되어 있습니다. 태그를 사용하고 있습니다. –

0

프로젝트에서 최대 절전 모드 지속성을 사용하는 모든 리소스를 나열하는 매핑 태그를 잊어 버린 것 같습니다. 여기

은 예입니다 :

있는 hibernate.cfg.xml

<session-factory> 

    <!-- Database connection settings --> 
    <property name="connection.driver_class">org.h2.Driver</property> 
    <property name="connection.url">jdbc:h2:file:db/personh2db;DB_CLOSE_DELAY=-1;MVCC=TRUE</property> 
    <property name="connection.username">sa</property> 
    <property name="connection.password"/> 

    <!-- JDBC connection pool (use the built-in) --> 
    <property name="connection.pool_size">1</property> 

    <!-- SQL dialect --> 
    <property name="dialect">org.hibernate.dialect.H2Dialect</property> 

    <!-- Disable the second-level cache --> 
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> 

    <!-- Echo all executed SQL to stdout --> 
    <property name="show_sql">true</property> 

    <!-- Drop and re-create the database schema on startup --> 
    <property name="hbm2ddl.auto">create</property> 

    <mapping resource="com/example/model/Person.hbm.xml"/> 
    <mapping resource="com/example/model/Properties.hbm.xml"/> 

</session-factory> 

+0

정말 도움이되는 매핑 태그를 추가하는 것을 잊었습니다! 그러나 나는 여전히 몇 가지 문제가 있습니다. 먼저 Client라는 누락 된 엔티티 클래스에 대한 오류가 있습니다 (Java 파일에서 @Entity 태그를 주석 처리했기 때문에). 그래서 Client.java 파일에서 Entity 태그를 다시 추가하고 "메인"스레드에서 예외가 발생했습니다. org.hibernate.AnnotationException : 엔티티에 식별자가 지정되지 않았습니다 : com.luv2code.hibernate.demo.entity.Client ".. 이 시점에서 필자는 .hbm.xml 스타일이 아닌 최대 절전 모드의 주석 스타일로 돌아가고 있다고 생각합니다. 생각? – AHijaouy

관련 문제