多个项目中的实体

3

我有一个很好的Spring项目。现在,由于需要在另一个项目中使用现有项目的一部分,我需要将项目拆分成两个部分。在拆分过程中,我遇到了一个问题,但我不理解这个问题的来源,也不知道该如何解决。例如:

+ -- myTestProject
  | -- PersonEntity
  | -- persistence.xml
  | -- pom.xml
+ -- myBaseProject
  |
  | -- PhoneEntity
  | -- pom.xml

当我将PhoneEntity保存在myTestProject中时,可以顺利启动Tomcat。当我像上面那样分离实体时,我会从EclipseLink收到以下错误信息:

对于实体类[class com.smith.Company]上的属性[phoneEntity]的类型[class com.smith.PhoneEntity]不是可序列化映射的有效类型。属性类型必须实现Serializable接口。

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">
    <persistence-unit name="eclipselink" transaction-type="RESOURCE_LOCAL">   

        <description>Application managed persistence unit</description>
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <non-jta-data-source>java:comp/env/jdbc/ecoiDS</non-jta-data-source>   
        <class>com.smith.PersonEntity</class>
        <class>com.smith.PhoneEntity</class>
        <class>com.smith.Company</class>

        <properties>
            <property name="eclipselink.logging.level" value="WARNING" />
            <property name="eclipselink.logging.timestamp" value="true" />
            <property name="eclipselink.logging.session" value="false" />
            <property name="eclipselink.logging.thread" value="false" />
            <property name="eclipselink.logging.exceptions" value="true" />
            <property name="eclipselink.weaving" value="static"/>
            <property name=""/>

        </properties>
    </persistence-unit>
</persistence>

persistence.xml文件已经包含了PhoneEntity。整个项目编译正常,但是无法启动项目。是否有任何提示可以解决这个问题?


你应该展示persistence.xml的相关部分。答案可能就在那里。 - davidxxx
已更新persistence.xml - LeO
将项目拆分成两个部分,你指的是“部分”是什么意思?如果你指的是模块,那么可以为jpa类创建第三个模块,包括实体和persistence.xml。 - Kh.Taheri
1
“模块”是指Java9-“模块”吗?或者你用它表示什么?“分成两个部分”意味着我有一个现有的项目,它应该仍然运行。然后在基础项目中雕刻出一些基础 - 可以在另一个项目中重复使用。这导致某些实体属于基础项目,而其他实体则不属于。但最重要的是,我不知道“Eclipselink”在抱怨什么。因为所有实体都在“persistence.xml”中提到。 - LeO
谢谢,请检查我刚才在答案中写的内容。 - Kh.Taheri
1个回答

4
您正在尝试创建一个多模块项目
我建议您创建第三个模块,将所有实体和persistence.xml文件添加到其中,并在您的BaseTest模块中添加该模块的依赖。
您可以在这里找到一个实际的例子 -> Spring Boot 多模块项目示例

所以你的意思是有一个包含所有实体的“EntityProject”,并在“persitence.xml”中收集它们。例如,JPARepro<PhoneEntity, Integer>进入“Base”项目。然后我可以在“Test”项目中使用它们 - 只需添加依赖项即可?我是否正确理解了“pom.xml”中主要差异是<packaging>属性? - LeO
就像这样在 <dependencies> 中添加依赖项:<dependency> <groupId>com.project</groupId> <artifactId>db_model</artifactId> </dependency> - Kh.Taheri
我更喜欢按照我在答案中提到的Spring教程进行操作,这将花费大约30分钟,然后您就会发现如何仅通过maven管理项目 ;) - Kh.Taheri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接