Skip to content

Remove Entity in Many-To-One (Cassandra) #484

@ngayngo9x

Description

@ngayngo9x

I remove Person, but Kundera not auto remove Address. And show exception:

Exception in thread "main" java.lang.NullPointerException
at com.impetus.kundera.proxy.cglib.CglibLazyInitializer.invoke(CglibLazyInitializer.java:286)
at com.pheu.learn.entities.Person1$$EnhancerByCGLIB$$8d97acb6.toString()

Person1 p1 = new Person1();
p1.setPersonId("1");
p1.setPersonName("MrPheu");

Person1 p2 = new Person1();
p2.setPersonId("2");
p2.setPersonName("MrOng");

Address1 a1 = new Address1();
a1.setAddressId("111");
a1.setStreet("HCM");
a1.setPerson(p1);

Address1 a2 = new Address1();
a2.setAddressId("222");
a2.setStreet("HN");
a2.setPerson(p2);

Address1 a3 = new Address1();
a3.setAddressId("333");
a3.setStreet("HN");
a3.setPerson(p1);

// insert 
em.persist(a1);
em.persist(a2);
em.persist(a3);

// delete
 em.remove(p1);

// query
List<?> aAll = em.createQuery("SELECT a FROM Address1 a").getResultList();
for (Object a : aAll) {
       Address1 address = (Address1) a;
       System.out.println("Address: " + address .toString());
   Person1 person = address.getPerson();
   if(person != null) {
           System.out.println("Person: " + person.toString());  Show exception here
   }
 }

Entities:

@Entity
@Table(name = "person1")
public class Person1 {

@Id
@Column(name = "person_id")
private String personId;

@Column(name = "person_name")
private String personName;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "person")
private Set<Address1> addresses;
    // Constructors, Getters, setters here
   }


 @Entity
 @Table(name = "address1")
 public class Address1 {
@Id
@Column(name = "address_id")
private String addressId;

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

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "person_id")
private Person1 person;
// Constructors, getters, setters here
  }

Persistence xml:

 <code>
 <?xml version="1.0" encoding="UTF-8"?>
 <persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="cassandra_pu">
    <provider>com.impetus.kundera.KunderaPersistence</provider>
    <class>com.pheu.learn.entities.Person</class>
    <class>com.pheu.learn.entities.Address</class>
    <class>com.pheu.learn.entities.Person1</class>
    <class>com.pheu.learn.entities.Address1</class>
    <class>com.pheu.learn.entities.Person2</class>
    <class>com.pheu.learn.entities.Address2</class>
    <class>com.pheu.learn.entities.Person3</class>
    <class>com.pheu.learn.entities.Address3</class>
    <properties>
        <property name="kundera.nodes" value="127.0.0.1" />
        <property name="kundera.port" value="9160" />
        <property name="kundera.keyspace" value="hello" />
        <property name="kundera.dialect" value="cassandra" />
        <property name="kundera.ddl.auto.prepare" value="create" />
        <property name="kundera.client.lookup.class"
            value="com.impetus.client.cassandra.pelops.PelopsClientFactory" />
        <property name="kundera.cache.provider.class"
            value="com.impetus.kundera.cache.ehcache.EhCacheProvider" />
        <property name="kundera.cache.config.resource" value="/ehcache-test.xml" />
        <property name="kundera.client.property" value="kundera-cassandra.xml" />
        <!-- <property name="index.home.dir" value="true"/> -->
    </properties>

    </persistence-unit>
     </persistence>

Kundera-cassandra.xml:

   <code>
   <?xml version="1.0" encoding="UTF-8"?>
   <clientProperties>
<datastores>
    <dataStore>
        <name>cassandra</name>
        <connection>
            <properties>
                <property name="cql.version" value="3.0.0"></property>
            </properties>
        </connection>
        <schemas>
            <schema>
                <name>hello</name>
                <properties>
                    <property name="strategy.class" value="SimpleStrategy" />
                    <property name="replcation.factor" value="1" />
                    <property name="durable.writes" value="true" />
                    <property name="index.home.dir" value="true" />
                </properties>
            </schema>
        </schemas> 
    </dataStore>
      </datastores>
      </clientProperties>
     </code>

Please help me.... T.T

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions