diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/xml/CassandraMappingContextParser.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/xml/CassandraMappingContextParser.java index 8cba96d6f..8a46de0c9 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/xml/CassandraMappingContextParser.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/xml/CassandraMappingContextParser.java @@ -25,6 +25,7 @@ import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.data.cassandra.config.CassandraEntityClassScanner; import org.springframework.data.cassandra.config.DefaultBeanNames; import org.springframework.data.cassandra.mapping.BasicCassandraMappingContext; import org.springframework.data.cassandra.mapping.EntityMapping; @@ -64,6 +65,18 @@ public class CassandraMappingContextParser extends AbstractSingleBeanDefinitionP protected void parseMapping(Element element, BeanDefinitionBuilder builder) { + String packages = element.getAttribute("entity-base-packages"); + if (StringUtils.hasText(packages)) { + try { + Set> entityClasses = CassandraEntityClassScanner.scan(StringUtils + .commaDelimitedListToStringArray(packages)); + builder.addPropertyValue("initialEntitySet", entityClasses); + } catch (Exception x) { + throw new IllegalArgumentException(String.format( + "encountered exception while scanning for entity classes in package(s) [%s]", packages), x); + } + } + Set mappings = new HashSet(); for (Element entity : DomUtils.getChildElementsByTagName(element, "entity")) { diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/xmlentityscanning/AnotherScannedEntity.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/xmlentityscanning/AnotherScannedEntity.java new file mode 100644 index 000000000..f7e3e74f7 --- /dev/null +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/xmlentityscanning/AnotherScannedEntity.java @@ -0,0 +1,69 @@ +/* + * Copyright 2013-2014 the original author or authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.cassandra.test.integration.xmlentityscanning; + +import org.springframework.data.annotation.Id; +import org.springframework.data.cassandra.mapping.Table; + +@Table +public class AnotherScannedEntity { + + /* + * Primary Row ID + */ + @Id private String name; + + @SuppressWarnings("unused") + private AnotherScannedEntity() {} + + public AnotherScannedEntity(String name) { + setName(name); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + AnotherScannedEntity other = (AnotherScannedEntity) obj; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + +} diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/xmlentityscanning/AnotherScannedEntityRepository.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/xmlentityscanning/AnotherScannedEntityRepository.java new file mode 100644 index 000000000..745b269c7 --- /dev/null +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/xmlentityscanning/AnotherScannedEntityRepository.java @@ -0,0 +1,20 @@ +/* + * Copyright 2013-2014 the original author or authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.cassandra.test.integration.xmlentityscanning; + +import org.springframework.data.cassandra.repository.TypedIdCassandraRepository; + +public interface AnotherScannedEntityRepository extends TypedIdCassandraRepository {} diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/xmlentityscanning/ScannedEntity.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/xmlentityscanning/ScannedEntity.java new file mode 100644 index 000000000..3b59628aa --- /dev/null +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/xmlentityscanning/ScannedEntity.java @@ -0,0 +1,69 @@ +/* + * Copyright 2013-2014 the original author or authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.cassandra.test.integration.xmlentityscanning; + +import org.springframework.data.annotation.Id; +import org.springframework.data.cassandra.mapping.Table; + +@Table +public class ScannedEntity { + + /* + * Primary Row ID + */ + @Id private String name; + + @SuppressWarnings("unused") + private ScannedEntity() {} + + public ScannedEntity(String name) { + setName(name); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ScannedEntity other = (ScannedEntity) obj; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + +} diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/xmlentityscanning/ScannedEntityRepository.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/xmlentityscanning/ScannedEntityRepository.java new file mode 100644 index 000000000..10e63ae01 --- /dev/null +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/xmlentityscanning/ScannedEntityRepository.java @@ -0,0 +1,20 @@ +/* + * Copyright 2013-2014 the original author or authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.cassandra.test.integration.xmlentityscanning; + +import org.springframework.data.cassandra.repository.TypedIdCassandraRepository; + +public interface ScannedEntityRepository extends TypedIdCassandraRepository {} diff --git a/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/xmlentityscanning/XmlEntityScanningIntegrationTests.java b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/xmlentityscanning/XmlEntityScanningIntegrationTests.java new file mode 100644 index 000000000..2518ea6fc --- /dev/null +++ b/spring-data-cassandra/src/test/java/org/springframework/data/cassandra/test/integration/xmlentityscanning/XmlEntityScanningIntegrationTests.java @@ -0,0 +1,44 @@ +/* + * Copyright 2013-2014 the original author or authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.cassandra.test.integration.xmlentityscanning; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.cassandra.core.CassandraOperations; +import org.springframework.data.cassandra.test.integration.support.AbstractSpringDataEmbeddedCassandraIntegrationTest; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +public class XmlEntityScanningIntegrationTests extends AbstractSpringDataEmbeddedCassandraIntegrationTest { + + @Autowired ScannedEntityRepository repository; + @Autowired AnotherScannedEntityRepository anotherRepository; + + @Autowired CassandraOperations template; + + @Test + public void testInsertScannedEntity() { + repository.save(new ScannedEntity("one")); + } + + @Test + public void testInsertAnotherScannedEntity() { + anotherRepository.save(new AnotherScannedEntity("another")); + } +} diff --git a/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/repository/UserRepositoryXmlConfigIntegrationTests-context.xml b/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/repository/UserRepositoryXmlConfigIntegrationTests-context.xml index a03322f83..d5f909fcc 100644 --- a/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/repository/UserRepositoryXmlConfigIntegrationTests-context.xml +++ b/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/repository/UserRepositoryXmlConfigIntegrationTests-context.xml @@ -21,12 +21,12 @@ - - diff --git a/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/xmlentityscanning/XmlEntityScanningIntegrationTests-context.xml b/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/xmlentityscanning/XmlEntityScanningIntegrationTests-context.xml new file mode 100644 index 000000000..8df2fe3d4 --- /dev/null +++ b/spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/test/integration/xmlentityscanning/XmlEntityScanningIntegrationTests-context.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + +