thymeleaf: Some partition key parts are missing and Invocation of init method failed

Author: aharo24

Originally Sourced from: https://stackoverflow.com/questions/74372773/thymeleaf-some-partition-key-parts-are-missing-and-invocation-of-init-method-fa


I have 2 Root Cause Issues/Errors

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-11-09 01:17:36.240 ERROR 20994 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'inboxApp': Invocation of init method failed; nested exception is org.springframework.data.cassandra.CassandraInvalidQueryException: Query; CQL [INSERT INTO emails_by_user (isunread,suject,"to") VALUES (?,?,?)]; Some partition key parts are missing: user_id, label; nested exception is com.datastax.oss.driver.api.core.servererrors.InvalidQueryException: Some partition key parts are missing: user_id, label

1

Caused by: org.springframework.data.cassandra.CassandraInvalidQueryException: Query; CQL [INSERT INTO emails_by_user (isunread,suject,"to") VALUES (?,?,?)]; Some partition key parts are missing: user_id, label; nested exception is com.datastax.oss.driver.api.core.servererrors.InvalidQueryException: Some partition key parts are missing: user_id, label

2

Caused by: com.datastax.oss.driver.api.core.servererrors.InvalidQueryException: Some partition key parts are missing: user_id, label
  • When creating the io.aharo.inbox.emaillist I began getting errors with the database.

  • It is supposed to generate 10 folders but fails to generate anything.

  • Only the titles of the Header titles get generated but no information gets added onto the database.

  • I am unsure where the errors might be, I tried using test functions to catch the error but with no effect.


EmailListItem.java

@Table(value = "emails_by_user")
public class EmailListItem 
{

    @PrimaryKey                                                            // repository system design pattern
    private EmailListItemKey key;

    @CassandraType(type = Name.LIST, typeArguments = Name.TEXT)
    private List<String> to;
    
    @CassandraType(type = Name.TEXT)
    private String suject;

    @CassandraType(type = Name.BOOLEAN)
    private boolean isUnread;

    
    public EmailListItem(EmailListItemKey key, List<String> to, String suject, boolean isUnread) {
        this.key = key;
        this.to = to;
        this.suject = suject;
        this.isUnread = isUnread;
    }

    public EmailListItem(){}
    

    public EmailListItemKey getKey() {
        return key;
    }

    public void setKey(EmailListItemKey key) {
        this.key = key;
    }

    public List<String> getTo() {
        return to;
    }

    public void setTo(List<String> to) {
        this.to = to;
    }

    public String getSuject() {
        return suject;
    }

    public void setSuject(String suject) {
        this.suject = suject;
    }

    public boolean isUnread() {
        return isUnread;
    }

    public void setUnread(boolean isUnread) {
        this.isUnread = isUnread;
    }    
}

EmailListItemKey.java

@PrimaryKeyClass
public class EmailListItemKey 
{
    @PrimaryKeyColumn(name = "user_id", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
    private String id;
    
    @PrimaryKeyColumn(name = "label", ordinal = 1, type = PrimaryKeyType.PARTITIONED)
    private String label; // folder label
   
    @PrimaryKeyColumn(name = "created_time_uuid", ordinal = 2, type = PrimaryKeyType.CLUSTERED)
    private UUID timeUuid;

    

    public EmailListItemKey(String id, String label, UUID timeUuid) {
        this.id = id;
        this.label = label;
        this.timeUuid = timeUuid;
    }

    public EmailListItemKey(){}

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public UUID getTimeUuid() {
        return timeUuid;
    }

    public void setTimeUuid(UUID timeUuid) {
        this.timeUuid = timeUuid;
    }

    
    
}

EmailListItemRepository.java

// @Repository
public interface EmailListItemRepository extends CassandraRepository<EmailListItem, EmailListItemKey> 
{
    //List<EmailListItem> findAllById(EmailListItemKey id);   // EmailListItemKey --> key we need to fetch <:?:> EmailListItem --> what we get back    
    List<EmailListItem> findAllByKey_IdAndKey_label(String id, String label);       // we dont want to map uuid soo we are updating the two paramerts we want
}

InboxController.java

@Controller
public class InboxController 
{
    @Autowired private FolderRepository folderRepository;
    @Autowired private FolderService folderService;

    @GetMapping(value = "/")
    //@RequestMapping("/")
    public String homePage(
            @AuthenticationPrincipal OAuth2User principal,
            Model model     // use model for templating/access;

    ) {

        if (principal == null || !StringUtils.hasText(principal.getAttribute("login")) )
        {
            return "index";
        }

        // fetch the folders
        String userId = principal.getAttribute("login");
       
        List<Folder> userFolders = folderRepository.findAllById(userId);
        model.addAttribute("userFolders", userFolders);

        List<Folder> defaulFolders = folderService.fetchDefaultFolders(userId);
        model.addAttribute("defaultFolders", defaulFolders);

        // fetch messages
        String folderlabel = "Inbox";
        // I added these code to see if it fixes defaultFolders
            // if (userFolders.size() > 0) {
            //     model.addAttribute("userFolders", userFolders);
            // }

        return "inbox-page";
    }
    
}

InboxApp.java Driver_Class

@SpringBootApplication
@RestController

public class InboxApp
{
    @Autowired 
    private FolderRepository folderRepository;
    
    @Autowired
    private EmailListItemRepository emailListItemRepository;



    public static void main(String[] args) {
        SpringApplication.run(InboxApp.class, args);
    }

    /**
     * This is neccesary for the Spring Boot App to use the Astra secure bundle
     * && connect to our database.
     */
    @Bean
    public CqlSessionBuilderCustomizer sessionBuilderCustomizer (DataStaxAstraProperties astraProperties)
    {
        Path bundle = astraProperties.getSecureConnectBundle().toPath();
        return builder -> builder.withCloudSecureConnectBundle(bundle);
    }

    @PostConstruct
    public void init()
    {
        folderRepository.save( new Folder("aharo24","Inbox", "blue"));              // this is how we see data  <--> make the paths 
        folderRepository.save( new Folder("aharo24","Sent", "green"));
        folderRepository.save( new Folder("aharo24","Important", "yellow "));

        for (int i=0; i < 10; ++ i)
        {
            EmailListItemKey key = new EmailListItemKey();
            key.setId("aharo24");
            key.setLabel("Inbox");
            key.setTimeUuid(Uuids.timeBased());                                                 // use api for time 

            EmailListItem item = new EmailListItem();
            item.setTo(Arrays.asList("aharo24"));
            item.setSuject("Subject" + i);
            item.setUnread(true);

            emailListItemRepository.save(item);
        }
    }

    // @RequestMapping("/user")
    // public String user(@AuthenticationPrincipal OAuth2User principal) {
    //  System.out.println(principal);
    //  return principal.getAttribute("name");
    // }
}

Environment Tree

  • I am unable to show pictures but my projects work tree environment looks like this.
.
├── mvnw
├── mvnw.cmd
├── pom.xml
├── README.md
├── src
│  └── main
│     ├── java
│     │  └── io
│     │     └── aharo
│     │        └── inbox
│     │           ├── controllers
│     │           │  └── InboxController.java
│     │           ├── DataStaxAstraProperties.java
│     │           ├── emaillist
│     │           │  ├── EmailListItem.java
│     │           │  ├── EmailListItemKey.java
│     │           │  └── EmailListItemRepository.java
│     │           ├── folders
│     │           │  ├── Folder.java
│     │           │  ├── FolderRepository.java
│     │           │  └── FolderService.java
│     │           ├── InboxApp.java
│     │           └── SecurityAdapter.java
│     └── resources
│        ├── application.yml
│        ├── META-INF
│        │  └── additional-spring-configuration-metadata.json
│        ├── secure-connect.zip
│        └── templates
│           ├── components
│           │  └── folder-list.html
│           ├── inbox-page.html
│           └── index.html
└── target
   ├── classes
   │  ├── application.yml
   │  ├── io
   │  │  └── aharo
   │  │     └── inbox
   │  │        ├── controllers
   │  │        │  └── InboxController.class
   │  │        ├── DataStaxAstraProperties.class
   │  │        ├── emaillist
   │  │        │  ├── EmailListItem.class
   │  │        │  ├── EmailListItemKey.class
   │  │        │  └── EmailListItemRepository.class
   │  │        ├── folders
   │  │        │  ├── Folder.class
   │  │        │  ├── FolderRepository.class
   │  │        │  └── FolderService.class
   │  │        ├── InboxApp.class
   │  │        └── SecurityAdapter.class
   │  ├── META-INF
   │  │  └── additional-spring-configuration-metadata.json
   │  ├── secure-connect.zip
   │  └── templates
   │     ├── components
   │     │  └── folder-list.html
   │     ├── inbox-page.html
   │     └── index.html
   ├── generated-sources
   │  └── annotations
   ├── maven-status
   │  └── maven-compiler-plugin
   │     └── compile
   │        └── default-compile
   └── test-classes

COMPLETE exception stack trace

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-11-09 00:30:18.396 ERROR 19973 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'inboxApp': Invocation of init method failed; nested exception is org.springframework.data.cassandra.CassandraInvalidQueryException: Query; CQL [INSERT INTO emails_by_user (isunread,suject,"to") VALUES (?,?,?)]; Some partition key parts are missing: user_id, label; nested exception is com.datastax.oss.driver.api.core.servererrors.InvalidQueryException: Some partition key parts are missing: user_id, label
        at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:160) ~[spring-beans-5.3.8.jar:5.3.8]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:422) ~[spring-beans-5.3.8.jar:5.3.8]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1778) ~[spring-beans-5.3.8.jar:5.3.8]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602) ~[spring-beans-5.3.8.jar:5.3.8]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.8.jar:5.3.8]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.8.jar:5.3.8]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.8.jar:5.3.8]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.8.jar:5.3.8]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.8.jar:5.3.8]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.8.jar:5.3.8]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.8.jar:5.3.8]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.8.jar:5.3.8]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.2.jar:2.5.2]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-2.5.2.jar:2.5.2]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-2.5.2.jar:2.5.2]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) ~[spring-boot-2.5.2.jar:2.5.2]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.2.jar:2.5.2]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) ~[spring-boot-2.5.2.jar:2.5.2]
        at io.aharo.inbox.InboxApp.main(InboxApp.java:117) ~[classes/:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
        at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.5.2.jar:2.5.2]
Caused by: org.springframework.data.cassandra.CassandraInvalidQueryException: Query; CQL [INSERT INTO emails_by_user (isunread,suject,"to") VALUES (?,?,?)]; Some partition key parts are missing: user_id, label; nested exception is com.datastax.oss.driver.api.core.servererrors.InvalidQueryException: Some partition key parts are missing: user_id, label
        at org.springframework.data.cassandra.core.cql.CassandraExceptionTranslator.translate(CassandraExceptionTranslator.java:125) ~[spring-data-cassandra-3.2.2.jar:3.2.2]
        at org.springframework.data.cassandra.core.cql.CassandraAccessor.translate(CassandraAccessor.java:422) ~[spring-data-cassandra-3.2.2.jar:3.2.2]
        at org.springframework.data.cassandra.core.cql.CqlTemplate.translateException(CqlTemplate.java:764) ~[spring-data-cassandra-3.2.2.jar:3.2.2]
        at org.springframework.data.cassandra.core.cql.CqlTemplate.query(CqlTemplate.java:545) ~[spring-data-cassandra-3.2.2.jar:3.2.2]
        at org.springframework.data.cassandra.core.CassandraTemplate.doExecute(CassandraTemplate.java:967) ~[spring-data-cassandra-3.2.2.jar:3.2.2]
        at org.springframework.data.cassandra.core.CassandraTemplate.doExecute(CassandraTemplate.java:955) ~[spring-data-cassandra-3.2.2.jar:3.2.2]
        at org.springframework.data.cassandra.core.CassandraTemplate.executeSave(CassandraTemplate.java:906) ~[spring-data-cassandra-3.2.2.jar:3.2.2]
        at org.springframework.data.cassandra.core.CassandraTemplate.executeSave(CassandraTemplate.java:897) ~[spring-data-cassandra-3.2.2.jar:3.2.2]
        at org.springframework.data.cassandra.core.CassandraTemplate.doInsert(CassandraTemplate.java:708) ~[spring-data-cassandra-3.2.2.jar:3.2.2]
        at org.springframework.data.cassandra.core.CassandraTemplate.doInsert(CassandraTemplate.java:691) ~[spring-data-cassandra-3.2.2.jar:3.2.2]
        at org.springframework.data.cassandra.core.CassandraTemplate.insert(CassandraTemplate.java:672) ~[spring-data-cassandra-3.2.2.jar:3.2.2]
        at org.springframework.data.cassandra.repository.support.SimpleCassandraRepository.save(SimpleCassandraRepository.java:96) ~[spring-data-cassandra-3.2.2.jar:3.2.2]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
        at org.springframework.data.repository.core.support.RepositoryMethodInvoker$RepositoryFragmentMethodInvoker.lambda$new$0(RepositoryMethodInvoker.java:289) ~[spring-data-commons-2.5.2.jar:2.5.2]
        at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137) ~[spring-data-commons-2.5.2.jar:2.5.2]
        at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121) ~[spring-data-commons-2.5.2.jar:2.5.2]
        at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:529) ~[spring-data-commons-2.5.2.jar:2.5.2]
        at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:285) ~[spring-data-commons-2.5.2.jar:2.5.2]
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:599) ~[spring-data-commons-2.5.2.jar:2.5.2]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.8.jar:5.3.8]
        at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:163) ~[spring-data-commons-2.5.2.jar:2.5.2]
        at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:138) ~[spring-data-commons-2.5.2.jar:2.5.2]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.8.jar:5.3.8]
        at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80) ~[spring-data-commons-2.5.2.jar:2.5.2]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.8.jar:5.3.8]
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) ~[spring-aop-5.3.8.jar:5.3.8]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.8.jar:5.3.8]
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215) ~[spring-aop-5.3.8.jar:5.3.8]
        at jdk.proxy3/jdk.proxy3.$Proxy124.save(Unknown Source) ~[na:na]
        at io.aharo.inbox.InboxApp.initializeData(InboxApp.java:148) ~[classes/:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
        at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:389) ~[spring-beans-5.3.8.jar:5.3.8]
        at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:333) ~[spring-beans-5.3.8.jar:5.3.8]
        at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:157) ~[spring-beans-5.3.8.jar:5.3.8]
        ... 23 common frames omitted
Caused by: com.datastax.oss.driver.api.core.servererrors.InvalidQueryException: Some partition key parts are missing: user_id, label
        at com.datastax.oss.driver.api.core.servererrors.InvalidQueryException.copy(InvalidQueryException.java:48) ~[java-driver-core-4.11.2.jar:na]
        at com.datastax.oss.driver.internal.core.util.concurrent.CompletableFutures.getUninterruptibly(CompletableFutures.java:149) ~[java-driver-core-4.11.2.jar:na]
        at com.datastax.oss.driver.internal.core.cql.CqlPrepareSyncProcessor.process(CqlPrepareSyncProcessor.java:59) ~[java-driver-core-4.11.2.jar:na]
        at com.datastax.oss.driver.internal.core.cql.CqlPrepareSyncProcessor.process(CqlPrepareSyncProcessor.java:31) ~[java-driver-core-4.11.2.jar:na]
        at com.datastax.oss.driver.internal.core.session.DefaultSession.execute(DefaultSession.java:230) ~[java-driver-core-4.11.2.jar:na]
        at com.datastax.oss.driver.api.core.cql.SyncCqlSession.prepare(SyncCqlSession.java:206) ~[java-driver-core-4.11.2.jar:na]
        at org.springframework.data.cassandra.core.CassandraTemplate$PreparedStatementHandler.createPreparedStatement(CassandraTemplate.java:1078) ~[spring-data-cassandra-3.2.2.jar:3.2.2]
        at org.springframework.data.cassandra.core.cql.CqlTemplate.query(CqlTemplate.java:531) ~[spring-data-cassandra-3.2.2.jar:3.2.2]