3

我是昆德拉的新手。我的用例是 Kundera 和 MySQL。我正在使用 spring 的 CrudRepository 保存一个实体。该实体中的 ID 是自动生成的。昆德拉在数据库中成功地保存了该对象。但返回错误的 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>

持久性.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>  
4

0 回答 0