Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DSClientFactory: AbstractClass Inheritance - Error while creating an instance of class #1022

Open
Angoth opened this issue Jul 11, 2018 · 8 comments

Comments

@Angoth
Copy link

Angoth commented Jul 11, 2018

Hi,
I'm currently evaluating Kundera with Datastax Cassandra and ran into some trouble reading my saved object. When I call entityManager.find, I get an ERROR c.i.k.u.KunderaCoreUtils - Error while creating an instance of class exception. The problem is, Kundera calls the Abstract Class constructor via reflections instead of the subclasses' contructors.

I've attached a simple example project with the problem.

Setup:
Datastax Enterprise Version 5.0.3
Cassandra 3.0
Kundera: 3.13
Driver: com.impetus.kundera.client.cassandra.dsdriver.DSClientFactory

Relevant classes:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "computer_type", discriminatorType = DiscriminatorType.STRING)
public abstract class AbstractComputer extends AbstractEntity {

	@ManyToOne
	private Rack rack;

	@Column
	private double price;

	public Rack getRack() {
		return rack;
	}

	public void setRack(Rack rack) {
		this.rack = rack;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

}
@Entity
public class Rack extends AbstractEntity {

	@OneToMany(cascade = { CascadeType.ALL }, mappedBy = "rack")
	@JoinColumn(name = "rack")
	private List<AbstractComputer> computers;

	public List<AbstractComputer> getComputers() {
		return computers;
	}

	public void setComputers(List<AbstractComputer> computers) {
		this.computers = computers;
	}
}
@Entity
@DiscriminatorValue(value = "ComputerA")
public class ComputerA extends AbstractComputer {

	@Column
	private Date date;

	@Column
	private String manufacturer;

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getManufacturer() {
		return manufacturer;
	}

	public void setManufacturer(String manufacturer) {
		this.manufacturer = manufacturer;
	}
}
@Entity
@DiscriminatorValue(value = "ComputerB")
public class ComputerB extends AbstractComputer {

	@Column
	private BigDecimal tact;

	@Column
	private int cores;

	public BigDecimal getTact() {
		return tact;
	}

	public void setTact(BigDecimal tact) {
		this.tact = tact;
	}

	public int getCores() {
		return cores;
	}

	public void setCores(int cores) {
		this.cores = cores;
	}
}

TestCase:

@Test
public void testDB() {
	EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
	EntityManager em = factory.createEntityManager();

	Rack rack = new Rack();
	List<AbstractComputer> computers = new ArrayList<>();

	ComputerA computerA = new ComputerA();
	computerA.setDate(new Date());
	computerA.setManufacturer("HP");

	ComputerB computerB = new ComputerB();
	computerB.setCores(4);
	computerB.setTact(new BigDecimal("4.99"));

	computers.add(computerA);
	computers.add(computerB);
	rack.setComputers(computers);

	em.persist(rack);
	em.flush();
	em.clear();

	Rack loadedRack = em.find(Rack.class, rack.getId());
	loadedRack.getComputers(); // error

	em.close();
	factory.close();
}

KunderaIssue.zip

@devender-yadav
Copy link
Contributor

Hi @Angoth, I will try to replicate this at my end.

@Angoth
Copy link
Author

Angoth commented Jul 16, 2018

Hi @devender-yadav,
thanks for looking into the problem. Any news about the issue?

@devender-yadav
Copy link
Contributor

Hi @Angoth, I got busy with other tasks. I will get back to you on this by tomorrow. Meanwhile can you please check these entites and testcase related to Inheritance - https://github.com/Impetus/Kundera/tree/c645d7f7e949a0e9f9b06b010e527725a4b0a3d2/src/kundera-cassandra/cassandra-core/src/test/java/com/impetus/kundera/client/crud/inheritence

@Angoth
Copy link
Author

Angoth commented Jul 16, 2018

Thanks for your fast response.
I already checked these testcases and had the same problem with DsClient. ThriftClient was working though.

But in my next evaluation step I had some really nasty performance problems and timeout exceptions while persisting a rather complex data model and large datasets on a 4 nodes cluster. Interestingly the performance problems did't occur with DsClient.

But the performance problems are another topic... Would be nice to find a solution to the DsClient issue and I'll be awaiting your answer on the issue

@Angoth
Copy link
Author

Angoth commented Jul 16, 2018

I did some debugging and perhaps here is some helpful information for you...

DsClient doesn't seem to have any check for SubTypes. In my opinion it should be in this method:

    private List iterateAndReturn(ResultSet rSet, EntityMetadata metadata) {
        MetamodelImpl metaModel =
            (MetamodelImpl) kunderaMetadata.getApplicationMetadata().getMetamodel(metadata.getPersistenceUnit());
        EntityType entityType = metaModel.entity(metadata.getEntityClazz());

        Iterator<Row> rowIter = rSet.iterator();
        List results = new ArrayList();

        Map<String, Object> relationalValues = new HashMap<String, Object>();

        while (rowIter.hasNext()) {
            Object entity = null;
            Row row = rowIter.next();
            populateObjectFromRow(metadata, metaModel, entityType, results, relationalValues, entity, row);
        }
        return results;
    }

Whereas ThriftClient is looking for SubTypes and creates the Entities with this information:

    public List<Object> findByRelation(String colName, Object colValue, Class entityClazz) {

            ...

            if (keySlices != null)
            {
                entities = new ArrayList<Object>(keySlices.size());

                MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata().getMetamodel(
                        m.getPersistenceUnit());

                EntityType entityType = metaModel.entity(m.getEntityClazz());

                List<AbstractManagedType> subManagedType = ((AbstractManagedType) entityType).getSubManagedType();

                if (subManagedType.isEmpty())
                {
                    entities = populateData(m, keySlices, entities, m.getRelationNames() != null, m.getRelationNames());
                }
                else
                {
                    for (AbstractManagedType subEntity : subManagedType)
                    {
                        EntityMetadata subEntityMetadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata,
                                subEntity.getJavaType());
                        entities = populateData(subEntityMetadata, keySlices, entities,
                                subEntityMetadata.getRelationNames() != null, subEntityMetadata.getRelationNames());
                        // TODOO:: if(entities != null)

                    }
                }
            }
        }
        return entities;
    }

@devender-yadav
Copy link
Contributor

@Angoth you are right. Implementation for inheritance is not there for DS driver. I am working on it. I need to handle find, query, and relations. Will update you once I make those changes.

You can also contribute this feature if you like to work on it.

@Angoth
Copy link
Author

Angoth commented Jul 18, 2018

I thought about it and looked into it but I'm rather busy at the moment, sorry. So I will wait for your solution :)

@devender-yadav
Copy link
Contributor

Sure 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants