I'm doing a POC on a Hibernate OGM implementation for Neo4j. In doing this, I've created the following objects:
#Entity
public class AutoDealership {
private String name;
public AutoDealership(String nam){
this.name = nam;
}
#Id
public String getName(){
return name;
}
private void setName(String n){
name = n;
}
private List<Vehicle> vehicle = new ArrayList<Vehicle>();
#OneToMany(mappedBy="autoDealership")
public List<Vehicle> getVehicle(){
return vehicle;
}
private void setVehicles(List<Vehicle> v){
this.vehicle = v;
}
}
And a Vehicle class:
public abstract class Vehicle {
protected AutoDealership autoDealership;
abstract int getValue();
#ManyToOne
public abstract AutoDealership getAutoDealership();
private void setValue(int v){
}
}
Which is extended into classes that have simple implementations. I run this code as follows:
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
OgmSession session =em.unwrap(OgmSession.class);
Camry camry1 = new Camry("Joe");
Camry camry2 = new Camry("Elvis");
Silverado s = new Silverado("TX");
Maserati mas = new Maserati();
mas.setStatus("Massive");
session.saveOrUpdate(camry1);
session.saveOrUpdate(camry2);
session.saveOrUpdate(s);
session.saveOrUpdate(mas);
AutoDealership a = new AutoDealership("Slick Willie's");
session.saveOrUpdate(a);
camry1.setAutoDealership(a);
camry2.setAutoDealership(a);
mas.setAutoDealership(a);
s.setAutoDealership(a);
tx.commit();
em.close();
emf.close();
When I run this, it fails with :
Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: ogm-neo4j] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1239)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.access$600(EntityManagerFactoryBuilderImpl.java:120)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:855)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:845)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:844)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:75)
at org.hibernate.ogm.jpa.HibernateOgmPersistence.createEntityManagerFactory(HibernateOgmPersistence.java:62)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at dogs.TestClass.main(TestClass.java:22)
Caused by: org.hibernate.AnnotationException: Use of #OneToMany or #ManyToMany targeting an unmapped class: cars.AutoDealership.vehicle[cars.Vehicle]
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1134)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:793)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:728)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:70)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1697)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1426)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
If I comment out anything related to getting the vehicles from AutoDealership, the code compiles and works, and successfully inserts nodes into the database. I've tried to change the mappedBy target to no avail.
It turns out that the problem was that I had to declare Vehicle as an Entity.
Related
I'm trying to implement domain event publishing from an entity by following the examples mentioned on the post below:
Example for #DomainEvents and #AfterDomainEventsPublication
However I haven't managed to have Spring calling my method annotated with #TransactionalEventListener.
See below the entity, service, event listener and test code:
#Entity
public class Book extends AbstractAggregateRoot<Book>
{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#Column(unique = true)
private String isbn;
#Column
private String name;
public Book(String isbn, String name)
{
this.isbn = isbn;
this.name = name;
}
public void purchase()
{
registerEvent(new BookPurchasedEvent(id));
}
// getters omitted for brevity
}
Service:
#Service
#Transactional
public class BookService
{
private final BookRepository bookRepository;
public BookService(BookRepository bookRepository)
{
this.bookRepository = bookRepository;
}
public void purchaseBook(Integer bookId)
{
Book book = bookRepository.findById(bookId)
.orElseThrow(NoSuchElementException::new);
book.purchase();
bookRepository.save(book);
}
}
Listener:
#Service
public class EventListener
{
private final Logger logger = LoggerFactory.getLogger(this.getClass());
#TransactionalEventListener
public void handleEvent(BookPurchasedEvent event)
{
logger.info("Received event {}", event);
}
}
Test:
#RunWith(SpringRunner.class)
#SpringBootTest
#Transactional
public class BookEventsTest
{
#Autowired
private BookService bookService;
#Autowired
private EntityManager entityManager;
#Test
public void test()
{
Book book = new Book("abcd-efgh", "El Quijote");
book = entityManager.merge(book);
bookService.purchaseBook(book.getId());
}
}
The log message from the listener is not logged. It works though when deployed as a REST service and invoked e.g. via Postman
Got it. Since my test is annotated with #Transactional, the transaction wrapping the test method will be rolled back. Therefore the method annotated with #TransactionalEventListener won't be called, since by default it triggers at the phase TransactionPhase.AFTER_COMMIT (and I'm not interested in having it called unless the transaction is successful). So the working version of the test looks as follows:
#RunWith(SpringRunner.class)
#SpringBootTest
public class BookEventsTest
{
#Autowired
private BookService bookService;
#Autowired
private BookRepository bookRepository;
#MockBean
private EventListener eventListener;
private Book book;
#Before
public void init() {
book = bookRepository.save(new Book("abcd-efgh", "El Quijote"));
}
#After
public void clean() {
bookRepository.deleteAll();
}
#Test
public void testService()
{
bookService.purchaseBook(book.getId());
then(eventListener)
.should()
.handleEvent(any(BookPurchasedEvent.class));
}
}
I'm trying to develop e very simple app based on a running thread creating entities in a DB every second in JavaEE on a Glassfish4 Server.
I'm using an Automatic Timer, where I inject an EJB managing the persistence.
The Timer Service is the following one:
#Singleton
#LocalBean
#Startup
public class UpdateEJB {
#EJB
MeasureEJB measureEjb;
#Schedule(second = "*/1", minute = "*", hour = "*", persistent = false)
public void doWork() {
measureEjb.create(new Measure());
}
}
While the EJB is:
#Stateless
public class MeasureEJB {
#PersistenceContext(unitName = "smarthomePU")
private EntityManager em;
public Measure create (Measure _measure) {
em.persist(_measure);
return _measure;
}
}
The Entity
#Entity
public class Measure implements Serializable {
#Id
private String time;
private int[] temp;
private boolean[] water;
public int[] getTemp() {
return temp;
}
public void setTemp(int[] temp) {
this.temp = temp;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public boolean[] getWater() {
return water;
}
public void setWater(boolean[] water) {
this.water = water;
}
private static final long serialVersionUID = 1L;
public Measure() {
super();
}
public Measure(int[] _temp, boolean[] _water) {
temp = _temp;
water = _water;
time = "";
}
}
But when I try to use the MeasureEJB in the UpdateEJB the app stops with a NPE on the create method. What am I doing wrong?
From the code fragments above, I cannot see how EntityManager is injected and since your're talking about an NPE the problem may lie there.
Also avoid #Singleton(s) if you don't keep state in your beans. By default your public void doWork() is associated with a Write Lock automatically.
I am using namedquery for rest api using Spring JPA. The named query is implemented in my entity class:
#Entity
#Table(name="SPECIMEN_TB")
#NamedQueries({
#NamedQuery(name="SpecimenTb.findBySpecimenNo", query="select s from SpecimenTb s where s.specimenNo = :specimenNo"),
})
public class SpecimenTb implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SPECIMEN_TB_ROWID_GENERATOR")
#Column(name="ROW_ID")
private long rowId;
#Column(name="SPECIMEN_NO", unique = true)
private String specimenNo;
My controller looks like this:
#RestController
public class RistoreController {
#Autowired
private RistoreService ristoreService;
#RequestMapping(
value = "/ristore/foundation/{specno}",
method = RequestMethod.GET,
produces = "application/json")
public ResponseEntity<SpecimenTb> getFmSpecimen(#PathVariable("specno") String specno) {
List<SpecimenTb> specimens = ristoreService.findBySpecimenNo(specno);
if (specimens == null) {
return new ResponseEntity<SpecimenTb>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<SpecimenTb>(specimens.get(0), HttpStatus.OK);
}
I have a service bean which calls JPA repository findBySpecimenNo method.
#Service
public class RistoreServiceBean implements RistoreService {
#Autowired
private SpecimenRepository specimenRepository;
#Override
public List<SpecimenTb> findAll() {
List<SpecimenTb> specimens = specimenRepository.findAll();
return specimens;
}
#Override
public List<SpecimenTb> findBySpecimenNo(String specimenNo) {
List<SpecimenTb> specimens = specimenRepository.findBySpecimenNo(specimenNo);
return specimens;
}
When I start the Spring Boot Application and type in the url "http://localhost:8080/ristore/foundation/SKM1", I got the following error:
java.lang.IllegalArgumentException: Parameter with that position [1] did not exist
What did I do wrong?
Looks like you can't use a named parameter with the #NamedQuery based on the docs I read. Have you tried with ?1 instead?
Reason that named parameter doesn't work is that you also have to add the annotation on the method parameter so Spring knows which parameter matches to what placeholder in the query.
I have a simple WebSocket set up and try to save data. Somehow the data gets not persisted. I don't get any error messages and the object gets returned correct to the client. If I try to store the object with a REST controller and a REST request it works.
Here are the dependencies of my build.gradle file:
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
compile 'org.springframework.boot:spring-boot-starter-websocket'
compile 'org.springframework:spring-messaging'
compile 'com.google.code.gson:gson:1.7.2'
compile 'org.postgresql:postgresql:9.4-1200-jdbc41'
compile 'commons-dbcp:commons-dbcp:1.4'
testCompile('org.springframework.boot:spring-boot-startet-test')
}
PersonController
#Controller
public class PersonController {
#Autowired
PersonRepository personRepository;
#MessageMapping("/test")
#SendTo("/response/test")
public Person test() throws Exception {
Person person = new Person();
person.setName("John Doe");
return personRepository.save(person);
}
}
Configuration for STOMP messaging
#Configuration
#EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
#Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/response");
config.setApplicationDestinationPrefixes("/app");
}
#Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/websocket")
.setAllowedOrigins("*")
.withSockJS();
}
Person entity
#Entity
public class Person {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return getName;
}
public void setName(String name) {
this.name = name;
}
}
Base Repository
#NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable> extends Repository<T, ID> {
void delete(T deleted);
void delete(ID id);
Iterable<T> findAll();
T findOne(ID id);
T save(T persisted);
Iterable<T> save(Iterable<T> persited);
}
Person Repository
public interface PersonRepository extends
BaseRepository<Person, Serializable> {
}
Is there a problem in my code?
Is there an issue with caching? Do I have to force flushing?
Is storing data with WebSockets supported by SpringBoot?
Do you know any examples with storing data? I could only find basic examples without storing data.
The problem was in my persistence configuration. I changed the configuration from a Java implementation to the application.properties file. I think there was a problem with my transaction manager.
To be complete, here is my current application.properties file:
spring.datasource.url = jdbc:postgresql://localhost:5432/test
spring.datasource.username = test
spring.datasource.password = test
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = create
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
I bow to the apt stackoverflow community and humbly seek guidance (I bow my head in subservience as I write this)
I have the following Entity class/bean which has a mixture of JPA/EclipseLink/JAXB/Moxy annotations: (btw EventBase is just a #MappedSuperclass that holds additional fields)
#Entity
#Table(name = "EVENTS")
#XmlRootElement
public class Event extends EventBase {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#XmlAttribute(name = "id")
private long eventCID;
#OneToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "APPLICATIONCID")
private CustomerApplication customerApplication;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "CUSTOMERCID")
private Customer customer;
....
}
Here's my code to marshal this entity (outer class excluded for brevity)
public static void main(String args[]) {
Event event = myInstance.populateEvent();
myInstance.buildXMLFromEvent(event);
}
public Event populateEvent() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory(this.persistenceUnit);
EntityManager em = null;
Event event = null;
try {
em = emf.createEntityManager();
event = (Event) em.createQuery("Select object(e) from Event e where e.eventCID = 55000").getSingleResult();
em.clear();
em.detach(event);
em.close();
em = null;
emf.close();
emf = null;
} catch (Exception e) { // just test code so catching general exception
log.error("Unexpected error: " + e);
} finally {
if (em != null) {
em.clear();
em.close();
}
}
return event;
}
private void buildXMLFromEvent(Event event) {
System.out.println("Marshalling now:");
JAXBContext jc;
try {
jc = JAXBContext.newInstance(Event.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.valueOf(true));
JAXBElement<Event> jaxbElement = new JAXBElement<Event>(new QName("event"), Event.class, event);
marshaller.marshal(jaxbElement, System.out);
} catch (JAXBException e) {
}
}
The generated xml actually goes and eagerly fetches all of the member objects of my Event entity bean! i.e) Customer, CustomerApplication and any other mappings which I've excluded for brevity. I'm using EclipseLink as my JPA provider and Moxy for JAXB. What am I doing wrong here? You can see that not only are the entityManager AND entityManagerFactory instances cleared, closed and set to null, but I've also gone ahead and detached the root Event entity. Moreover, the fetchtype has explicitly been set to LAZY!
How is it that JAXB can eagerly fetch when the Event object has been detached? I thought closing the entityManager in itself detaches all managed objects? Is there some cached session context that JAXB is hanging on to? If so, why is it not even honoring the explicitly defined fetch strategy?
Many thanks in advance!
Ustad
If eclipselink is in standalone mode, then only ManyToMany and OneToMany relations is actually uses lazy-loading, for others fetch attribute is ignored and equal to EAGER.
Here is documentation for that http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings/Lazy_Basics
One-to-one: By default, EclipseLink JPA ignores the fetch attribute and
default javax.persistence.FetchType.EAGER applies.
Many-to-one: EclipseLink JPA performs lazy loading when the fetch
attribute is set to javax.persistence.FetchType.LAZY.
Basic: By default, EclipseLink JPA ignores the fetch attribute and
default javax.persistence.FetchType.EAGER applies.
That`s why your entities are loaded with relations.
Hope it helps.