I've got two jars, dog.jar and person.jar, both packaged as ejb modules within a single ear. Dog.java is an Entity that references another Entity Person.java via a OneToMany mapping on an "owner" field. Each jar has its own persistence.xml, which reference a common persistence unit, but each persistence.xml only contains the classes contained in that jar.
Upon startup, Glassfish complains:
Exception Description: [class com.example.dog.entities.Dog] uses a non-entity [class com.example.person.entities.Person] as target entity in the relationship attribute [field owner].
It seems to work if I add the Person class to the dog jar's persistence.xml, but I don't like that at all. The Person class is already defined in the persistence.xml of the person.jar, which is in same named persistence unit, in the same ear, so it should find it at runtime! I don't want to repeat myself.
Plus, I use the handy Eclipse JPA tooling to auto-sycnhronize the class names in the persistence.xml, and when I do that it only finds the Dog.java when I run it on dog's persistence.xml. So I don't want to have to hunt down all references and manually add them to persistence.xml and worry about them getting blown away when I resynchronize.
Also I made sure I put the person ejb module before the dog ejb module in the application.xml.
This is my first foray into JPA and Java EE, so I could be doing something wrong... I drank the koolaid by reading books and articles but nobody seems to ever show a good mutli-module enterprise example...
Related
I'm seeking other's thoughts on a specific scenario re: unit testing domain model (entities, VOs etc) attributes in a App that is utilising CQRS.
Imagine a simplified example of a Product (entity), with a Product Name (VO).
class Product
private ProductName productName
Product has some basic guard rails and invariants:
A Product Name can't be more than 100 characters (enforced in the VO).
A Product must have a Product Name (enforced in the entity, with Product Name set in the constructor to ensure it's always valid).
I can unit test the Product entity and Product Name VO to ensure these are enforced correctly by ensuring the correct errors and exceptions are raised. Easy.
My question is regarding Unit Testing the happy path — that the Product Name is set successfully when a Product is initially Developed.
In a non-CQRS system I would have a public read-only property or getter on the Product Name, so that it can be retrieved for reporting, displaying or populating a DTO. I can then unit test with this property.
In a CQRS system though, the Product Name can be private in the Entity, as retrieval of the Product Name happens via the Read Model on the query side. Setting the Product Name happens through the command when the product is Developed, but after that the Product Name isn't required for any core business actions (except I could imagine for renaming the product).
To unit test the successful creation of a product though I need to make the Product Name a read-only public property to test it, but it doesn't seem right to do this just to fulfil a unit test. Without the unit test the Product Name can remain private and everything would work as needed.
Wondering if anyone has come across a similar scenario for unit testing -> Where an attribute that is mostly for presentation purposes (although obviously very important... I wouldn't expect users to try and work with Products via only part numbers or identifiers) is being tested?
I'm leaning towards testing this via the Read Model? ie. Create the Product , then determine the name has been successfully set via the Read Model. But this seems to be a lot for a unit test to take on.
To unit test the successful creation of a product though I need to make the Product Name a read-only public property to test it, but it doesn't seem right to do this just to fulfil a unit test. Without the unit test the Product Name can remain private and everything would work as needed.
How does data get from your domain model to your persistent storage? How does data get from your "write" model to your "read" model?
Somewhere in your code base you have either a function, that takes a Product as an input and returns some domain agnostic representation of it (byte[], or JSON, or whatever), or you have a method that takes as arguments a Product and a callback that accepts some domain agnostic representation.
It might be explicit, or it might be implicit (ORM magic?), but it's going to be there somewhere -- "write only" domain entities aren't very interesting.
Your tests should be using that same mechanism.
It may help to keep in mind one of the old understandings of "unit" in "unit test" -- that the test is a single isolated self contained thing, insulated from interference by other tests that may also be running.
I'm developing web application using symfony 3. I'm quite new with symfony. I devided my application by bundles. But sometimes I need entities from other bundle. So my question is - should I place entities to some CommonBundle or is it ok to use entities from other bundles?
I would say it is okay to import (use) an entity from another bundle. Keep in mind that this creates a one-way dependency across bundles. If in the other bundle you also import entities (or anything else) from the first bundle, you end up with two-way dependency - in this case the bundles are dependant on each other, and it's impossible to remove one, without modifying another.
I don't think common bundle will help you in this case. I myself also created like a CoreBundle on several projects, but it mostly contained interfaces or some abstractions, and it did not have any dependency on any other bundle.
Some people also suggest creating only one bundle for you app and decoupling the business code from the bundles. But if this is your first time using symfony, I would not recommend you do that.
we can make service to access entity in any bundle in the project
like :-
services:
test_project.list.view:
class: TestProject\TestBundle\Entity\ProfileSchool
arguments:
- "#service_container"
then access the this entity like this in controller :
$view = $this->container->get('test_project.list.view');
Note :
test_project.list.view can be any name,its just demo.
Hope this will help you.
let me know if any issue.
I have an existing Spring MVC + Spring Security + Thymeleaf project. My intention is to add Spring Lemon functionalities to it.
I followed Spring Lemon Getting Started guide, and built a Lemon-powered project. It runs successfully.
Now I'm trying to copy my entities into the Lemon project.
Things go well until I modify my entities to extend VersionedEntity, as said in the documentation.
Then I get this error :
![Error]http://i.stack.imgur.com/snz86.png
Looks like VersionedEntity is incompatible with my ManyToOne relationships. And when I delete those relationships, the problem disappears.
How do i get the tables generated with those JPA annotations ?
VersionedEntity is a lightweight class to support versioning, which extends LemonEntity, which in turn extends Spring Data JPA's AbstractAuditable. So, to pin point where could be the problem, I think you could try extending your classes straight from from LemonEntity or AbstractAuditable, and then see if the issue still remains.
Let's see what you find. If the issue comes even if your entities extend AbstractAuditable, maybe AbstractAuditable isn't compatible with #ManyToOne (assuming your code is fine). In that case, I think raising it with Spring Data JPA guys (either add spring-data-jpa tag to this question or create a separate question with that tag) will help.
Even extending AbstractAuditable didn't solve it. With the help of Sanjay, I understood that when you extend VersionedEntity or LemonEntity, you don't need the Id field in your entity class anymore. Then I deleted it, and it worked.
I just designed a web service (code firts approach) with one method with the following signature getItems(MyEntity myEntity) The point is that MyEntity has others Entities as properties and so on. Because of this, when the client generates the classes from the WSDL, lots of entities are created in the client side besides the xml (WSDL) generated is too complex, the question is; is it rigth to expose the entities of my sistem to the clients?, or should i create a entity with simple values? (int amount, String code, String user) like a facade.
I would recommend only exposing the properties and objects that you need. If there are properties on your MyEntity class that you will never expect the client to populate, there's no point in including that in the WSDL.
Is it possible to unit test Entity Framework v2 repositories with SqLite? Is this only possible if my entities are plain Poco and not automatically generated by Entity Framework?
I've generated a entity model from SqlServer and in the generated .edmx file i found this in section SSDL content: Provider="System.Data.SqlClient". Correct me if I am wrong, but shouldnt that be System.Data.SQLite in order to work with sqlite?
Its only possible if two entity models are created (one for each provider) and then make both object contexts implement the same interface.
Its not worth the effort to manage two entity models.
I hope this get fixed in Entity Framework Ver 3.