简单的用例与Kundera和MysQL

时间:2014-07-29 12:44:17

标签: java jpa kundera

我是kundera的新手。我的用例是Kundera和MySQL。我正在使用spring的CrudRepository保存一个实体。该实体中的ID是AUTO生成的。 Kundera正在DB中成功保存该对象。但返回错误的ID。如果我使用该Id从DB中查找数据,则返回null。

如果我遗漏了任何东西,请你告诉我。

实体类:

    @Entity
    @Table(name = "REQUEST_DETAILS")
    public class RequestDetails {

        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "ID")
        protected long id;

        @Column(name = "REQUEST_STATUS", nullable = false)
        private String requestStatus;

        // getter setter
    }

主要课程:

    public class Main {

        public static void main(String[] args) {

            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:/spring/jpa-config.xml");

            RequestDetailsRepository repository = context.getBean(RequestDetailsRepository.class);

            RequestDetails requestDetails = new RequestDetails();
            requestDetails.setRequestStatus("STARTED");

            RequestDetails updatedRequest = repository.save(requestDetails);
            System.out.println("request id : " + updatedRequest.getId());   // returning wrong ID (probably hashcode of that object)

           // Do some operation

           RequestDetails details = repository.findOne(updatedRequest.getId()); // returning NULL

    }

CRUD存储库:

    public interface RequestDetailsRepository extends org.springframework.data.repository.CrudRepository<RequestDetails, Long> {

    }

jpa-config.xml:

    <beans>

        <context:component-scan base-package="com.example.project" />

        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
                <property name="persistenceUnitName" value="persistenceUnit"/>
        </bean>

        <jpa:repositories base-package="com.example.project.repository"/>   

    </beans>

Persistence.xml:

    <persistence>
        <persistence-unit name="persistenceUnit">
            <provider>com.impetus.kundera.KunderaPersistence</provider>
            <properties>
                <property name="kundera.client.lookup.class" value="com.impetus.client.rdbms.RDBMSClientFactory" />
                <property name="hibernate.show_sql" value="true" />
                <property name="hibernate.format_sql" value="true" />
                <property name="hibernate.hbm2ddl.auto" value="update" />
                <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2008Dialect" />
                <property name="hibernate.connection.driver_class" value="net.sourceforge.jtds.jdbc.Driver" />
                <property name="hibernate.connection.url"
                    value="jdbc:jtds:sqlserver://10.127.127.215:1433;databaseName=myDB" />
                <property name="hibernate.connection.username" value="user" />
                <property name="hibernate.connection.password" value="password" />
                <property name="hibernate.current_session_context_class"
                    value="org.hibernate.context.ThreadLocalSessionContext" />
            </properties>
        </persistence-unit>
    </persistence>  

0 个答案:

没有答案