@OneToOne或@ManyToOne引用了来自springboot中不同应用程序和数据库的未知实体

bxgwgixi  于 2022-11-23  发布在  Spring
关注(0)|答案(1)|浏览(148)

我正在使用Springboot的一个应用程序中使用一些类,这些类在另一个应用程序中管理并存储在另一个数据库中。我的Flight类在本地数据库的当前应用程序中管理,它有一个Aircraft类型的属性,该属性也在当前应用程序中定义,但在另一个应用程序中管理并存储在另一个数据库中。
Flight类:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(schema = "app1")
public class Flight implements Serializable {
    @Id
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "flight_sequence"
    )
    @SequenceGenerator(
            name = "flight_sequence",
            allocationSize = 1
    )
    @Column(nullable = false, updatable = false)
    private Long id;

    private String callsign;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="aircraft_id", nullable=false)
    private Aircraft aircraft;
    private Date date;
    private Operator operator;
    private String origin;
    private String destination;
}

我的Aircraft类:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(schema = "app2")
public class Aircraft implements Serializable {
    @Id
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "aircraft_sequence"
    )
    @SequenceGenerator(
            name = "aircraft_sequence",
            allocationSize = 1
    )
    @Column(nullable = false, updatable = false)
    private Long id;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="operator_id", nullable=false)
    private Operator operator;
    private String registration;
    private String acType;
}

我在FlightRepository中编写了一个Jpa查询,它使用Flight类的Aircraft属性中的registration属性作为参数:

public interface FlightRepository extends JpaRepository<Flight, Long> {
    Flight findFirstByDestinationAndAircraftRegistrationOrderByDateDesc(String destination, String registration);
}

但这会导致以下异常:

Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.student.application.domain.app1.Flight.aircraft references an unknown entity: com.student.application.domain.app2.Aircraft
    at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:100)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processEndOfQueue(InFlightMetadataCollectorImpl.java:1750)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processFkSecondPassesInOrder(InFlightMetadataCollectorImpl.java:1694)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1623)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:295)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1460)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1494)
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800)
    ... 86 more

如果我在Aircraft类中添加@OneToMany注解,问题仍然存在。如果我同时删除@OneToMany@ManyToOne注解,我会得到Cannot join to attribute of basic type异常。
以下是两个数据库的配置类:
第一个

xtupzzrd

xtupzzrd1#

一个航班只能有一个飞机,但一个飞机可以有多个航班...因此,在飞机上,您应该有@OneToMany(mappedBy = "Flight")连接列,此处不需要
我假设你希望它是由航班Map,并有所有的信息存储在航班表。你也可以连接表,使航班_飞机表。
注解飞行中-〉@ManyToOne@JoinColumn(name="aircraft_id")

相关问题