spring data jpa, share entities over projects, unmapped entity





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I have a couple of services who use the same form of table to store translations, so I moved the translation entity into a shared project and try to have a unidirectional @OneToMany mapping on that entity. However I keep getting following exception
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.examples.blog.Post.translations[com.examples.shared.domain.Translation]



my Post class looks like this



package com.examples.blog.domain;

import com.examples.shared.domain.Translation;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

@Data
@Entity
@Table(name = "POSTS")
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Currency implements Serializable {

@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
private String id;

private String author;

@Embedded
private Source source;

@OneToMany(cascade = CascadeType.ALL,
orphanRemoval = true)
@JoinColumn(name = "entity_id")
List<Translation> translations;

}


and my shared Translation class looks like this:



package com.examples.shared.domain;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "TRANSLATIONS")
@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
public class Translation implements Serializable {

@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
private String id;

@Column(name = "language")
private String language;

@Column(name="translation")
private String translation;

@Column(name="entity_id")
@ManyToOne
private String entityId;

}


Anyone can help me figure out what I'm doing wrong here?










share|improve this question























  • I also have added the packages to my basePackages @EnableJpaRepositories(basePackages = {"com.examples"})

    – J.Pip
    Nov 16 '18 at 12:31


















1















I have a couple of services who use the same form of table to store translations, so I moved the translation entity into a shared project and try to have a unidirectional @OneToMany mapping on that entity. However I keep getting following exception
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.examples.blog.Post.translations[com.examples.shared.domain.Translation]



my Post class looks like this



package com.examples.blog.domain;

import com.examples.shared.domain.Translation;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

@Data
@Entity
@Table(name = "POSTS")
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Currency implements Serializable {

@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
private String id;

private String author;

@Embedded
private Source source;

@OneToMany(cascade = CascadeType.ALL,
orphanRemoval = true)
@JoinColumn(name = "entity_id")
List<Translation> translations;

}


and my shared Translation class looks like this:



package com.examples.shared.domain;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "TRANSLATIONS")
@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
public class Translation implements Serializable {

@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
private String id;

@Column(name = "language")
private String language;

@Column(name="translation")
private String translation;

@Column(name="entity_id")
@ManyToOne
private String entityId;

}


Anyone can help me figure out what I'm doing wrong here?










share|improve this question























  • I also have added the packages to my basePackages @EnableJpaRepositories(basePackages = {"com.examples"})

    – J.Pip
    Nov 16 '18 at 12:31














1












1








1








I have a couple of services who use the same form of table to store translations, so I moved the translation entity into a shared project and try to have a unidirectional @OneToMany mapping on that entity. However I keep getting following exception
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.examples.blog.Post.translations[com.examples.shared.domain.Translation]



my Post class looks like this



package com.examples.blog.domain;

import com.examples.shared.domain.Translation;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

@Data
@Entity
@Table(name = "POSTS")
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Currency implements Serializable {

@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
private String id;

private String author;

@Embedded
private Source source;

@OneToMany(cascade = CascadeType.ALL,
orphanRemoval = true)
@JoinColumn(name = "entity_id")
List<Translation> translations;

}


and my shared Translation class looks like this:



package com.examples.shared.domain;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "TRANSLATIONS")
@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
public class Translation implements Serializable {

@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
private String id;

@Column(name = "language")
private String language;

@Column(name="translation")
private String translation;

@Column(name="entity_id")
@ManyToOne
private String entityId;

}


Anyone can help me figure out what I'm doing wrong here?










share|improve this question














I have a couple of services who use the same form of table to store translations, so I moved the translation entity into a shared project and try to have a unidirectional @OneToMany mapping on that entity. However I keep getting following exception
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.examples.blog.Post.translations[com.examples.shared.domain.Translation]



my Post class looks like this



package com.examples.blog.domain;

import com.examples.shared.domain.Translation;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

@Data
@Entity
@Table(name = "POSTS")
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Currency implements Serializable {

@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
private String id;

private String author;

@Embedded
private Source source;

@OneToMany(cascade = CascadeType.ALL,
orphanRemoval = true)
@JoinColumn(name = "entity_id")
List<Translation> translations;

}


and my shared Translation class looks like this:



package com.examples.shared.domain;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "TRANSLATIONS")
@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
public class Translation implements Serializable {

@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
private String id;

@Column(name = "language")
private String language;

@Column(name="translation")
private String translation;

@Column(name="entity_id")
@ManyToOne
private String entityId;

}


Anyone can help me figure out what I'm doing wrong here?







spring hibernate jpa






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 12:24









J.PipJ.Pip

2,76572240




2,76572240













  • I also have added the packages to my basePackages @EnableJpaRepositories(basePackages = {"com.examples"})

    – J.Pip
    Nov 16 '18 at 12:31



















  • I also have added the packages to my basePackages @EnableJpaRepositories(basePackages = {"com.examples"})

    – J.Pip
    Nov 16 '18 at 12:31

















I also have added the packages to my basePackages @EnableJpaRepositories(basePackages = {"com.examples"})

– J.Pip
Nov 16 '18 at 12:31





I also have added the packages to my basePackages @EnableJpaRepositories(basePackages = {"com.examples"})

– J.Pip
Nov 16 '18 at 12:31












3 Answers
3






active

oldest

votes


















2














Seems like hibernate is not aware of com.examples.shared.domain.Translation entity.
You should provide hibernate a list of classes or packages where it should look for @Entities classes.



If you use Spring Boot use @EntityScan(basePackages="com.examples.shared.domain").



If you use Spring + Hibernate integration, use LocalContainerEntityManagerFactoryBean.setPackagesToScan("com.examples.shared.domain")



If you use plain hibernate, add a corresponding entry to persistence.xml or hibernate.cfg.xml:



<hibernate-configuration>
....
<mapping class="com.examples.shared.domain.Translation"/>
</session-factory>
</hibernate-configuration>


Or for java config see docs: http://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/Hibernate_User_Guide.html#bootstrap-bootstrap-native-registry-MetadataSources-example






share|improve this answer


























  • thank you this did the trick!

    – J.Pip
    Nov 16 '18 at 12:55



















2














Below statement is causing you issue:



  @Column(name="entity_id")
@ManyToOne
private String entityId;


Instead of String entityId, you should mention relationship with a valid entity class. Here entityId is of type String which is obviously not a declared entity.






share|improve this answer
























  • well as stated above the Translation Entity will be shared over many different services, so this time it's Post, next time it's MenuEntry, ... so how would I be able to do it like that?

    – J.Pip
    Nov 16 '18 at 12:38






  • 1





    In that case the simplest way would be to remove @ManytoOne annotation from entityid field

    – codeLover
    Nov 16 '18 at 12:45











  • tried it and it's still the same exception

    – J.Pip
    Nov 16 '18 at 12:48



















2














You are mapping 2nd class in onetomany in one class but mapping manytoone to a string instead of class.



Change



@ManyToOne
private String entityId;


to



@ManyToOne
private Currency entityId;


See this






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337878%2fspring-data-jpa-share-entities-over-projects-unmapped-entity%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Seems like hibernate is not aware of com.examples.shared.domain.Translation entity.
    You should provide hibernate a list of classes or packages where it should look for @Entities classes.



    If you use Spring Boot use @EntityScan(basePackages="com.examples.shared.domain").



    If you use Spring + Hibernate integration, use LocalContainerEntityManagerFactoryBean.setPackagesToScan("com.examples.shared.domain")



    If you use plain hibernate, add a corresponding entry to persistence.xml or hibernate.cfg.xml:



    <hibernate-configuration>
    ....
    <mapping class="com.examples.shared.domain.Translation"/>
    </session-factory>
    </hibernate-configuration>


    Or for java config see docs: http://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/Hibernate_User_Guide.html#bootstrap-bootstrap-native-registry-MetadataSources-example






    share|improve this answer


























    • thank you this did the trick!

      – J.Pip
      Nov 16 '18 at 12:55
















    2














    Seems like hibernate is not aware of com.examples.shared.domain.Translation entity.
    You should provide hibernate a list of classes or packages where it should look for @Entities classes.



    If you use Spring Boot use @EntityScan(basePackages="com.examples.shared.domain").



    If you use Spring + Hibernate integration, use LocalContainerEntityManagerFactoryBean.setPackagesToScan("com.examples.shared.domain")



    If you use plain hibernate, add a corresponding entry to persistence.xml or hibernate.cfg.xml:



    <hibernate-configuration>
    ....
    <mapping class="com.examples.shared.domain.Translation"/>
    </session-factory>
    </hibernate-configuration>


    Or for java config see docs: http://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/Hibernate_User_Guide.html#bootstrap-bootstrap-native-registry-MetadataSources-example






    share|improve this answer


























    • thank you this did the trick!

      – J.Pip
      Nov 16 '18 at 12:55














    2












    2








    2







    Seems like hibernate is not aware of com.examples.shared.domain.Translation entity.
    You should provide hibernate a list of classes or packages where it should look for @Entities classes.



    If you use Spring Boot use @EntityScan(basePackages="com.examples.shared.domain").



    If you use Spring + Hibernate integration, use LocalContainerEntityManagerFactoryBean.setPackagesToScan("com.examples.shared.domain")



    If you use plain hibernate, add a corresponding entry to persistence.xml or hibernate.cfg.xml:



    <hibernate-configuration>
    ....
    <mapping class="com.examples.shared.domain.Translation"/>
    </session-factory>
    </hibernate-configuration>


    Or for java config see docs: http://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/Hibernate_User_Guide.html#bootstrap-bootstrap-native-registry-MetadataSources-example






    share|improve this answer















    Seems like hibernate is not aware of com.examples.shared.domain.Translation entity.
    You should provide hibernate a list of classes or packages where it should look for @Entities classes.



    If you use Spring Boot use @EntityScan(basePackages="com.examples.shared.domain").



    If you use Spring + Hibernate integration, use LocalContainerEntityManagerFactoryBean.setPackagesToScan("com.examples.shared.domain")



    If you use plain hibernate, add a corresponding entry to persistence.xml or hibernate.cfg.xml:



    <hibernate-configuration>
    ....
    <mapping class="com.examples.shared.domain.Translation"/>
    </session-factory>
    </hibernate-configuration>


    Or for java config see docs: http://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/Hibernate_User_Guide.html#bootstrap-bootstrap-native-registry-MetadataSources-example







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 22 '18 at 9:27

























    answered Nov 16 '18 at 12:52









    StasKolodyukStasKolodyuk

    1,6261527




    1,6261527













    • thank you this did the trick!

      – J.Pip
      Nov 16 '18 at 12:55



















    • thank you this did the trick!

      – J.Pip
      Nov 16 '18 at 12:55

















    thank you this did the trick!

    – J.Pip
    Nov 16 '18 at 12:55





    thank you this did the trick!

    – J.Pip
    Nov 16 '18 at 12:55













    2














    Below statement is causing you issue:



      @Column(name="entity_id")
    @ManyToOne
    private String entityId;


    Instead of String entityId, you should mention relationship with a valid entity class. Here entityId is of type String which is obviously not a declared entity.






    share|improve this answer
























    • well as stated above the Translation Entity will be shared over many different services, so this time it's Post, next time it's MenuEntry, ... so how would I be able to do it like that?

      – J.Pip
      Nov 16 '18 at 12:38






    • 1





      In that case the simplest way would be to remove @ManytoOne annotation from entityid field

      – codeLover
      Nov 16 '18 at 12:45











    • tried it and it's still the same exception

      – J.Pip
      Nov 16 '18 at 12:48
















    2














    Below statement is causing you issue:



      @Column(name="entity_id")
    @ManyToOne
    private String entityId;


    Instead of String entityId, you should mention relationship with a valid entity class. Here entityId is of type String which is obviously not a declared entity.






    share|improve this answer
























    • well as stated above the Translation Entity will be shared over many different services, so this time it's Post, next time it's MenuEntry, ... so how would I be able to do it like that?

      – J.Pip
      Nov 16 '18 at 12:38






    • 1





      In that case the simplest way would be to remove @ManytoOne annotation from entityid field

      – codeLover
      Nov 16 '18 at 12:45











    • tried it and it's still the same exception

      – J.Pip
      Nov 16 '18 at 12:48














    2












    2








    2







    Below statement is causing you issue:



      @Column(name="entity_id")
    @ManyToOne
    private String entityId;


    Instead of String entityId, you should mention relationship with a valid entity class. Here entityId is of type String which is obviously not a declared entity.






    share|improve this answer













    Below statement is causing you issue:



      @Column(name="entity_id")
    @ManyToOne
    private String entityId;


    Instead of String entityId, you should mention relationship with a valid entity class. Here entityId is of type String which is obviously not a declared entity.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 16 '18 at 12:34









    codeLovercodeLover

    2,2551620




    2,2551620













    • well as stated above the Translation Entity will be shared over many different services, so this time it's Post, next time it's MenuEntry, ... so how would I be able to do it like that?

      – J.Pip
      Nov 16 '18 at 12:38






    • 1





      In that case the simplest way would be to remove @ManytoOne annotation from entityid field

      – codeLover
      Nov 16 '18 at 12:45











    • tried it and it's still the same exception

      – J.Pip
      Nov 16 '18 at 12:48



















    • well as stated above the Translation Entity will be shared over many different services, so this time it's Post, next time it's MenuEntry, ... so how would I be able to do it like that?

      – J.Pip
      Nov 16 '18 at 12:38






    • 1





      In that case the simplest way would be to remove @ManytoOne annotation from entityid field

      – codeLover
      Nov 16 '18 at 12:45











    • tried it and it's still the same exception

      – J.Pip
      Nov 16 '18 at 12:48

















    well as stated above the Translation Entity will be shared over many different services, so this time it's Post, next time it's MenuEntry, ... so how would I be able to do it like that?

    – J.Pip
    Nov 16 '18 at 12:38





    well as stated above the Translation Entity will be shared over many different services, so this time it's Post, next time it's MenuEntry, ... so how would I be able to do it like that?

    – J.Pip
    Nov 16 '18 at 12:38




    1




    1





    In that case the simplest way would be to remove @ManytoOne annotation from entityid field

    – codeLover
    Nov 16 '18 at 12:45





    In that case the simplest way would be to remove @ManytoOne annotation from entityid field

    – codeLover
    Nov 16 '18 at 12:45













    tried it and it's still the same exception

    – J.Pip
    Nov 16 '18 at 12:48





    tried it and it's still the same exception

    – J.Pip
    Nov 16 '18 at 12:48











    2














    You are mapping 2nd class in onetomany in one class but mapping manytoone to a string instead of class.



    Change



    @ManyToOne
    private String entityId;


    to



    @ManyToOne
    private Currency entityId;


    See this






    share|improve this answer




























      2














      You are mapping 2nd class in onetomany in one class but mapping manytoone to a string instead of class.



      Change



      @ManyToOne
      private String entityId;


      to



      @ManyToOne
      private Currency entityId;


      See this






      share|improve this answer


























        2












        2








        2







        You are mapping 2nd class in onetomany in one class but mapping manytoone to a string instead of class.



        Change



        @ManyToOne
        private String entityId;


        to



        @ManyToOne
        private Currency entityId;


        See this






        share|improve this answer













        You are mapping 2nd class in onetomany in one class but mapping manytoone to a string instead of class.



        Change



        @ManyToOne
        private String entityId;


        to



        @ManyToOne
        private Currency entityId;


        See this







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 12:34









        AlienAlien

        5,50331128




        5,50331128






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337878%2fspring-data-jpa-share-entities-over-projects-unmapped-entity%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python