You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot Data JPA中能否将Mongo实体关联到MySQL实体?

问题:Spring Boot中能否将MongoDB实体直接嵌入MySQL实体?

我已经在同一个Spring Boot服务中成功配置并运行了MongoDB和MySQL,但尝试把MongoDB的实体Contact添加到MySQL的实体SomeClass中时,抛出了映射错误,想请教这种跨数据源实体直接关联的操作是否可行?

MySQL实体(含尝试注释)

package com.lingar.SocialEvents.socialEvent.entities;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import com.lingar.SocialEvents.common.contact.Contact;
import com.lingar.SocialEvents.common.entities.TextPiece;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)

@Entity
//@Embeddable
@Table(name = "multi_props_values")
public class SomeClass extends ParentClass{
    private @Id 
    @GeneratedValue//(strategy = GenerationType.IDENTITY)//SO the genration will be in each entity seperate 
    Long id;
    @OneToOne(cascade = CascadeType.MERGE) //was @oneToOne. Here is the problem ? //TODO - Maybe should be one to many. Each Multi propName can be attached to many values. 
    private MultiPropName multiPropName;
    
    
////    @OneToMany
//  @ElementCollection
//  @CollectionTable//
////    private List<SinglePropValue> singlePropsValuesList = new ArrayList<>();//this is working
//  private List<Contact> contacts;//not working with mongo
    
    private Contact c = new Contact();
//  
    private String propValue;
    
    public MultiPropValue(){}
    public MultiPropValue(String propName, String propValue) {
        this.multiPropName = new MultiPropName(propName);
        this.propValue = propValue;
    }
    public MultiPropValue(MultiPropName multiPropName, String propValue) {
        super();
        this.multiPropName = multiPropName;
        
        this.propValue = propValue;
    }
    
    
    public MultiPropValue(MultiPropName multiPropName, String propValue, Contact c) {
        super();
        this.c = c;
        
        this.propValue = propValue;
    }       
    
}

MongoDB实体

package com.lingar.SocialEvents.common.contact;

//@RepositoryRestResource(collectionResourceRel = "people", path = "people")
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import com.lingar.SocialEvents.system.demo.mongo.MongoPersonTest;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
 * Contact object - bean
 * @author lingar 
 *
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document//(collection="mongo_person_test")
public class Contact {
 
    @Id
    private String id;

    private String name;
    
    private String value;

    public Contact(String name, String value) {
        this.name = name;
        this.value = value;
    }
    
    
}

错误信息

Caused by: org.hibernate.MappingException: Could not determine type for: com.lingar.SocialEvents.common.contact.Contact, at table: multi_props_values, for columns: [org.hibernate.mapping.Column(c)]

内容的提问来源于stack exchange,提问作者lingar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 22:17:47