简体   繁体   中英

Spring save composite primary key with foreign keys

I have a problem to create entity with composite primary key and this key is also foreign keys. This is my tables and relations tables schematics . When I want create news entity, I has receiving error message with null create newsTranslation. NewsTranslation have composite primary key with foreign key referenced to news table.

This is my code:

NewsAggregate

@AggregateRoot
@Entity
@Table(name = "news")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class NewsAggregate extends AuditingAggregateRoot implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "news_id")
    private Long newsId;

    @ManyToOne
    @JoinColumn(name = "id")
    private UserAggregate authorId;

    @Column(name = "publish_time")
    private ZonedDateTime publishTime;

    @Column(name = "published", nullable = false)
    private boolean published = false;

    @ManyToMany
    @JoinTable(
        name = "tags_news",
        joinColumns = {@JoinColumn(name = "news_id", referencedColumnName = "news_id")},
        inverseJoinColumns = {@JoinColumn(name = "tag", referencedColumnName = "tag")})
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<Tags> tags = new HashSet<Tags>();

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL,
        orphanRemoval = true)
    private List<NewsTranslation> newsTranslation;



    public NewsAggregate(UserAggregate authorId, Set<Tags> tags, String langCode,
                         String title, String shortDescription, String longDescription) {
        this.authorId = authorId;
        this.tags = tags;
        newsTranslation.add(
            new NewsTranslation(this, langCode, title, shortDescription, longDescription)
        );


    }

//Getters, equal, hashCode

NewsTranslation

@Entity
@Table(name = "news_translation")
/*@IdClass(NewsLangId.class)*/
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class NewsTranslation implements Serializable {

    private static final long serialVersionUID = 7576921328990303444L;

    @EmbeddedId
    private NewsLangId newsLangId;

    @Column(name = "title", length = TITLE_MAX_LENGTH, nullable = false)
    private String title;

    @Column(name = "short_description", length = SHORT_DESC_MAX_LENGTH, nullable = false)
    private String shortDescription;

    @Column(name = "long_description", nullable = false)
    private String longDescription;

    @ManyToOne
    @JoinColumn(name = "news_id", insertable = false, updatable = false)
    private NewsAggregate newsAggregate;

    @ManyToOne
    @JoinColumn(name = "lang_code", insertable = false, updatable = false)
    private Languages languages;

    public NewsTranslation() {
    }

    protected NewsTranslation(NewsAggregate newsAggregate, String langCode,String title, String shortDescription, String longDescription) {
        System.out.println(">>>>>>>>>>>>>>>.. NewsTranslation constructor");
        this.title = title;
        this.shortDescription = shortDescription;
        this.longDescription = longDescription;
        this.newsAggregate = newsAggregate;
        this.languages = new Languages();
        this.newsLangId = new NewsLangId(this.newsAggregate.getId(), langCode);
        System.out.println(">>>>>>>>> NewsTranslation : title = "+this.title+", shortDescription = "+this.shortDescription+
        ", longDescription = "+this.longDescription+", newsAggregate = "+this.newsAggregate +", languages = "+this.languages);

    }

// getters, setters

Tags

@Entity
@Table(name = "tags")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Tags implements Serializable {
private static final long serialVersionUID = 2995101252921266446L;

@Id
@Column(name = "tag", length = TAG_MAX_LENGTH, unique = true)
private String tag;

public Tags() {
}

private Tags(Builder builder) {
    this.tag = Preconditions.checkNotNull(builder.tag);
}

public void setTag(String tag) {
    this.tag = tag;
}

NewsLangId

@Embeddable
public class NewsLangId implements Serializable {

private static final long serialVersionUID = -4822628791720244835L;

@Column(name = "news_id")
private long newsId;
@Column(name = "lang_code")
private String langCode;

public NewsLangId() {
}

public NewsLangId(long newsId, String langCode) {
    System.out.println("Create NewsLangId: "+newsId+", "+langCode);
    this.newsId = newsId;
    this.langCode = langCode;
}

// getters, setters, equal, hashcode

In NewsFactory I want create NewsAggregate with NewsTranlation, but I have error message NullPointer.

NewsFactory

Set<Tags> tagsSet = new HashSet<Tags>(tagsRepository.findAll());
        UserAggregate login = userRepository.findOneByLogin("admin").get();


        NewsAggregate news = new NewsAggregate(login,tagsSet, createNewsDto.getLangCode(), createNewsDto.getTitle(),
            createNewsDto.getShortDescription(),createNewsDto.getLongDescription());

        log.info(">>>>>>>>>>>> NewsFactory: newsAggregate = "+news);

        return news;

Error:

java.lang.NullPointerException: null
    at it.seiton.aexea.domain.model.news.NewsTranslation.<init>(NewsTranslation.java:55)
    at it.seiton.aexea.domain.model.news.NewsAggregate.<init>(NewsAggregate.java:61)
    at it.seiton.aexea.domain.model.news.NewsFactory.createNews(NewsFactory.java:55)
    at it.seiton.aexea.domain.service.impl.NewsServiceDefault.createNews(NewsServiceDefault.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
    at com.sun.proxy.$Proxy184.createNews(Unknown Source)
    at it.seiton.aexea.web.rest.NewsResource.createNews(NewsResource.java:43)
    at it.seiton.aexea.web.rest.NewsResource$$FastClassBySpringC530334.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85)
    at it.seiton.aexea.aop.logging.LoggingAspect.logAround(LoggingAspect.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:620)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:609)
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:68)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:59)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
    at it.seiton.aexea.web.rest.NewsResource$$EnhancerBySpringCGLIB$$c9591bb7.createNews(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:817)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:731)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:968)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:870)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
    at com.codahale.metrics.servlet.AbstractInstrumentedFilter.doFilter(AbstractInstrumentedFilter.java:104)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
    at com.hazelcast.web.WebFilter.doFilter(WebFilter.java:360)
    at io.undertow..core.ManagedFilter.doFilter(ManagedFilter.java:60)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
    at org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration$ApplicationContextHeaderFilter.doFilterInternal(EndpointWebMvcAutoConfiguration.java:237)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
    at org.springframework.web.filter.CorsFilter.doFilterInternal(CorsFilter.java:92)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
    at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:112)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:207)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:176)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    at org.springframework.web.filter.OncePerRequestFilter.doOncePerRequestFilter.java:107)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:87)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
    at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.security.handlers.AbstractConfiityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
    at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.SessionRestoringHandler.handleRequest(SessionRestoringHandler.java:119)
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:284)
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:263)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:174)
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:793)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

PS. Sorry for my english.

I'm resolved my problem through change generated primary key. Now java generate id, not postgresql.

NewsAggregate

@AggregateRoot
@Entity
@Table(name = "news")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class NewsAggregate extends AuditingAggregateRoot implements Serializable {
    private static final Logger log = LoggerFactory.getLogger(NewsAggregate.class);


@Id
@Column(name = "news_id")
private String newsId;

@ManyToOne
@JoinColumn(name = "author_id", referencedColumnName = "id")
private UserAggregate authorId;

@Column(name = "publish_date")
private ZonedDateTime publishTime = null;

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
    name = "tags_news",
    joinColumns = {@JoinColumn(name = "news_id", referencedColumnName = "news_id")},
    inverseJoinColumns = {@JoinColumn(name = "tag", referencedColumnName = "tag")})
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<Tags> tags = new HashSet<Tags>();

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "news_id", referencedColumnName = "news_id")
@Fetch(FetchMode.JOIN)
private List<NewsTranslation> newsTranslation = new ArrayList<NewsTranslation>();

public NewsAggregate(Set<Tags> tags, UserAggregate authorId) {
    this.newsId = generateId(authorId.getId());
    this.tags = tags;
    this.authorId = authorId;
    this.publishTime = ZonedDateTime.now();
}

private NewsAggregate(Builder builder) {
    this.authorId = Preconditions.checkNotNull(builder.authorId);
    this.publishTime = builder.publishTime;
    this.tags = Preconditions.checkNotNull(builder.tags);
    this.newsTranslation = Preconditions.checkNotNull(builder.newsTranslation);
}

private String generateId(long userId) {
    return ZonedDateTime.now().toInstant().toEpochMilli() + "_" + userId;
}

public static NewsAggregate createNews(Set<Tags> tags, UserAggregate authorId, CreateNewsDto createNewsDto) {

    NewsAggregate news = new NewsAggregate(tags, authorId);
    log.info(">>>>>>>>>>>>>>>>>> news_id = "+news.getNewsId());
    NewsLangId newsLangId = new NewsLangId(news.getNewsId(), "en");

    NewsTranslation newsTranslation = new NewsTranslation(newsLangId, createNewsDto.getTitle(),
        createNewsDto.getShortDescription(), createNewsDto.getLongDescription());

    List<NewsTranslation> newsTranslations = new ArrayList<>();
    newsTranslations.add(newsTranslation);

    news.addTranslation(newsTranslations);
    return news;
}

private void addTranslation(List<NewsTranslation> newsTranslations) {
    this.newsTranslation = newsTranslations;
}

Thanks all for interesting. I hope this will be helpfull.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM