0

I know that I can run mongo and mysql from the same server, as explained here.

I do it, and it's working.

But now I'm trying to add some entity of Mongo to the Entity of the mysql.

In other way, I want to mix their implementation, after all it's logic that it would be accessible to the rest of the app.

When I'm doing it, it throws an error and don't allow it.

Is it possible?

My Code:

MySql entity (with some commented tries):

    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;
    }       
    
}

Mongo Entity:

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;
    }
    
    
}

The error:

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)]
4
  • Maybe this can't be done directly, but nothing is stopping you from writing an adaptor which can convert between the two entities. Commented Apr 3, 2023 at 10:39
  • You mean to get it as id's or something like that? Commented Apr 3, 2023 at 10:40
  • 1
    It is best practice to have one entity class/instance bound to one data source. Commented Apr 3, 2023 at 10:42
  • Thanks, BTW - vice versa - when I insert mysql entity in mongo it does working, so it looks weird. Commented Apr 3, 2023 at 10:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.