Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
registering hibernate entities from plugins
- Loading branch information
Showing
6 changed files
with
179 additions
and
16 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/main/java/br/com/caelum/calopsita/infra/vraptor/AnnotationConfigurationFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package br.com.caelum.calopsita.infra.vraptor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.annotation.PostConstruct; | ||
|
||
import org.hibernate.cfg.AnnotationConfiguration; | ||
|
||
import br.com.caelum.vraptor.ioc.ApplicationScoped; | ||
import br.com.caelum.vraptor.ioc.Component; | ||
import br.com.caelum.vraptor.ioc.ComponentFactory; | ||
|
||
@ApplicationScoped | ||
@Component | ||
public class AnnotationConfigurationFactory implements ComponentFactory<AnnotationConfiguration> { | ||
|
||
private static List<Class<?>> entities = new ArrayList<Class<?>>(); | ||
|
||
private AnnotationConfiguration configuration; | ||
|
||
public AnnotationConfigurationFactory() { | ||
configuration = new AnnotationConfiguration(); | ||
} | ||
|
||
@PostConstruct | ||
public void addEntitiesToConfiguration() { | ||
configuration.configure(); | ||
for (Class<?> entity : entities) { | ||
configuration.addAnnotatedClass(entity); | ||
} | ||
} | ||
|
||
static void addEntity(Class<?> entity) { | ||
entities.add(entity); | ||
} | ||
|
||
static List<Class<?>> getEntities() { | ||
return entities; | ||
} | ||
|
||
@Override | ||
public AnnotationConfiguration getInstance() { | ||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/br/com/caelum/calopsita/infra/vraptor/SessionFactoryCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/*** | ||
* Copyright (c) 2009 Caelum - www.caelum.com.br/opensource | ||
* All rights reserved. | ||
* | ||
* 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 br.com.caelum.calopsita.infra.vraptor; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.annotation.PreDestroy; | ||
|
||
import org.hibernate.SessionFactory; | ||
import org.hibernate.cfg.AnnotationConfiguration; | ||
|
||
import br.com.caelum.vraptor.ioc.ApplicationScoped; | ||
import br.com.caelum.vraptor.ioc.ComponentFactory; | ||
|
||
/** | ||
* Creates a SessionFactory from default resource /hibernate.cfg.xml, using | ||
* AnnotationConfiguration, and provides it to container | ||
* @author Lucas Cavalcanti | ||
* | ||
*/ | ||
@ApplicationScoped | ||
public class SessionFactoryCreator implements ComponentFactory<SessionFactory> { | ||
|
||
private SessionFactory factory; | ||
private final AnnotationConfiguration configuration; | ||
|
||
public SessionFactoryCreator(AnnotationConfiguration configuration) { | ||
this.configuration = configuration; | ||
} | ||
|
||
@PostConstruct | ||
public void create() { | ||
factory = configuration.buildSessionFactory(); | ||
} | ||
|
||
public SessionFactory getInstance() { | ||
return factory; | ||
} | ||
|
||
@PreDestroy | ||
public void destroy() { | ||
factory.close(); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/test/java/br/com/caelum/calopsita/infra/vraptor/AnEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package br.com.caelum.calopsita.infra.vraptor; | ||
|
||
import javax.persistence.Entity; | ||
|
||
@Entity | ||
public class AnEntity { | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/test/java/br/com/caelum/calopsita/infra/vraptor/AnnotationConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package br.com.caelum.calopsita.infra.vraptor; | ||
|
||
import static org.mockito.Mockito.verify; | ||
import net.vidageek.mirror.dsl.Mirror; | ||
|
||
import org.hibernate.cfg.AnnotationConfiguration; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
public class AnnotationConfigurationTest { | ||
|
||
private AnnotationConfigurationFactory factory; | ||
|
||
private @Mock AnnotationConfiguration configuration; | ||
private class MockedClass {}; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
MockitoAnnotations.initMocks(this); | ||
|
||
factory = new AnnotationConfigurationFactory(); | ||
|
||
new Mirror().on(factory).set().field("configuration").withValue(configuration); | ||
} | ||
|
||
@Test | ||
public void shouldAddGivenClassesToAnnotationConfiguration() throws Exception { | ||
factory.addEntity(MockedClass.class); | ||
factory.addEntitiesToConfiguration(); | ||
verify(configuration).addAnnotatedClass(MockedClass.class); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters