Skip to content

Commit

Permalink
feat: update contacts
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <[email protected]>
  • Loading branch information
otaviojava committed Aug 29, 2023
1 parent eb22245 commit a10e1e7
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*/

package expert.os.books.persistence.nosql.chapter13;


import jakarta.enterprise.inject.se.SeContainer;
import jakarta.enterprise.inject.se.SeContainerInitializer;
import jakarta.nosql.column.ColumnTemplate;
import org.eclipse.jnosql.databases.cassandra.mapping.CassandraTemplate;

import java.util.Arrays;
import java.util.Optional;


public class App {


public static void main(String[] args) {

try(SeContainer container = SeContainerInitializer.newInstance().initialize()) {

Person otaviojava = Person.builder().phones(Arrays.asList("123456", "432"))
.name("Name").id(1).build();

ColumnTemplate template = container.select(CassandraTemplate.class).get();
Person saved = template.insert(otaviojava);
System.out.println("Person saved" + saved);

Optional<Person> person = template.select(Person.class)
.where("id").eq(1L).singleResult();
System.out.println("Entity found: " + person);

}
}

private App() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import jakarta.nosql.Entity;
import jakarta.nosql.Id;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;


Expand All @@ -30,30 +32,27 @@ public class Person {
private String name;

@Column
private List<String> phones;
private Map<String, String> contacts;


public long getId() {
return id;
public Person() {
}

public String getName() {
return name;
Person(long id, String name, Map<String, String> contacts) {
this.id = id;
this.name = name;
this.contacts = contacts;
}


public List<String> getPhones() {
return phones;
public long id() {
return id;
}


public Person() {
public String name() {
return name;
}

Person(long id, String name, List<String> phones) {
this.id = id;
this.name = name;
this.phones = phones;
public Map<String, String> contacts() {
return Collections.unmodifiableMap(contacts);
}

@Override
Expand All @@ -78,7 +77,7 @@ public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", phones=" + phones +
", contacts=" + contacts +
'}';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
*/
package expert.os.books.persistence.nosql.chapter13;

import java.util.Collections;
import java.util.List;
import java.util.Map;

public class PersonBuilder {
private long id;
private String name;
private List<String> phones;
private Map<String, String> contacts = Collections.emptyMap();
PersonBuilder() {
}

Expand All @@ -29,12 +31,12 @@ public PersonBuilder name(String name) {
return this;
}

public PersonBuilder phones(List<String> phones) {
this.phones = phones;
public PersonBuilder contacts(Map<String, String> contacts) {
this.contacts = contacts;
return this;
}

public Person build() {
return new Person(id, name, phones);
return new Person(id, name, contacts);
}
}
2 changes: 1 addition & 1 deletion chapter-13/src/main/resources/cassandra.cql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE KEYSPACE IF NOT EXISTS developers WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3};
CREATE COLUMNFAMILY IF NOT EXISTS developers.Person (id bigint PRIMARY KEY, name text, phones list<text>);
CREATE COLUMNFAMILY IF NOT EXISTS developers.Person (id bigint PRIMARY KEY, name text, contacts map<text, text>);
CREATE TYPE IF NOT EXISTS developers.director (name text, movies set<text> );
CREATE COLUMNFAMILY IF NOT EXISTS developers.Movie (name text PRIMARY KEY, age int,director FROZEN<director>);
create index if not exists ageIndex on developers.movie(age);

0 comments on commit a10e1e7

Please sign in to comment.