How to call an entity class without primary key in JPA eclipseLink? - jpa

I am new to JPA eclipseLink.
I have two entity classes in header and LineItem details level. I can only call by ID in header to fetch the details from LineItem (Using OneToMany and ManyToOne). However, I have different cases where I need to call by name, customer account etc.
I have already tried with all suggestion from different forums but no luck.

Related

Fiware Orion-LD Auto Increment Entity Id

An Entity Id is required for each new entity in the broker.
In NGSI-LD the format for the Entity Id is urn:ngsi-ld:TemperatureSensor:001.
Is it possible to somehow autoincrement the number of the Entity so that the new Entity can be created programmatically?
For example, provide something like urn:ngsi-ld:TemperatureSensor:* and the new Entity can be created with an auto-incremented Entity Id.
Thanks.
The id should be generated by the application itself. You have multiple ways of generating unique ids but I would recommend ids that have some semantics behind so that you can, for instance, infer from an id in which area the sensor is located, who the owner is, etc.

ef code first conditional mapping

Is there a way to do conditional mapping in EF 6 with Code First.
We have a table called Entity which holds Suppliers, Customers and Employees. There is a flag field which identifies if a record is supplier, customer, employee or a combination of them. So we are using enum flags. In our code we have three domain objects (Supplier, Customer and Employee). I want to map these objects to the Entity table based on the flag field. Can somebody please tell me if this is possible in EF6. Please note that we are using Code First

JPA: Is it possible to persist child objects in the array only when the main object is persisted?

I am developing a web application using JSF and JPA(Eclipse link). I have two entities with bidirectional OneToMany relationship. The owner entity is contact and target entity is customer. Single customer can have multiple contacts, like email, phone, etc. When the end user is adding a new customer, he also adds the contacts straight away. There is a need to cancel the saving of a new customer, even after adding contacts to that customer. I tried to add that functionality, but failed in the following way.
Can that senario be achieved directly by persistence?
Contact Entity
....
public class Contact implements Serializable {
....
#ManyToOne
Customer customer;
....
Customer Entity
....
public class Customer implements Serializable {
....
#OneToMany(mappedBy = "customer")
private List<Contact> contacts;
....
Adding a new contact to Customer (current is an object of Customer class)
Contact contact = new Contact();
contact.setCustomer(current);
....
current.getInstitutionContacts().add(contact);
This works when the current is already a persisted one. If I tried to add a contact to yet to persist one, there is a java.lang.NullPointerException.
I can work around to achieve the functionality, but is there any way we can just collect the contacts to the array and persist them only when (and if only) the customer is persisted? By using cascade persist or lazy fetch, etc?
Sounds like you want Contacts to be Components, not Entities.
The difference is that an entity has it's own lifecycle; it lives outside the scope of its association, and deleting the parent does NOT necessarily have to delete the child. Also, if a child is an Entity, other classes can also have relationships with that child.
Components are completely bound to the parent. They automatically go away if the parent goes away. They cannot be referenced by other associations or by other Entities. It's like they are simple properties of the parent class.
The only caveat is that I don't know if all JPA implementations support having a collection of components.
See this documentation. Particularly the part that says: "You can also use association annotations in an embeddable object (ie #OneToOne, #ManyToOne, #OneToMany or #ManyToMany). To override the association columns you can use #AssociationOverride."
If the JPA implementation you are using does, you can use the #Embeddable annotation and #OneToMany
Edit: -- I also found info here http://en.wikibooks.org/wiki/Java_Persistence/Embeddables#Collections.

Entity Framework Error 3002 / 3003 - Inheritance / key mapping issue

I have a data model as follows:
A Customer has Products and Payment Methods. Each Product can be assigned any or all of the Customer's Payment Methods, with one set as default.
Foreign Keys are:
Customer.CustomerId => Product.CustomerId
Customer.CustomerId => PaymentMethod.CustomerId
Product.ProductId => ProductPaymentMethod.ProductId
PaymentMethod.PaymentMethodId => ProductPaymentMethod.PaymentMethodId
I want to customise this model for presentation purposes, Customer to have a collection of Payment Methods and a collection of Products. Products to have a collection of ProductPaymentMethods which inherit from PaymentMethod.
I deleted the association between PaymentMethod and ProductPaymentMethod, added an inheritence from PaymentMethod to ProductPaymentMethod and deleted PaymentMethodId from ProductPaymentMethod.
This is now my model:
When I save the model or build the project I get 2 errors:
Error 3002: Problem in mapping
fragments starting at line
226:Potential runtime violation of
table ProductPaymentMethod's keys
(ProductPaymentMethod.ProductPaymentMethodId):
Columns
(ProductPaymentMethod.ProductPaymentMethodId)
are mapped to EntitySet
PaymentMethods's properties
(PaymentMethods.ProductPaymentMethodId)
on the conceptual side but they do not
form the EntitySet's key properties
(PaymentMethods.PaymentMethodId).
and
Error 3003: Problem in mapping
fragments starting at line 226:All the
key properties
(PaymentMethods.PaymentMethodId) of
the EntitySet PaymentMethods must be
mapped to all the key properties
(ProductPaymentMethod.ProductPaymentMethodId)
of table ProductPaymentMethod.
What am I doing wrong?
EDIT: Having done some further Googling, I have found several solutions, most of which don't quite fit this scenario. Most talk about inheritance requiring a 1-1 not 1-many relationship. However, because of the Customer to Product 1-many relationship, the model requires a 1-many between PaymentMethod and ProfilePaymentMethod. Is it not possible to do what I am attempting?
My only answer to this so far is to have some manually created POCO classes for presentation and a Mapper class to turn my data entity into a presentation entity

Trouble inheriting from another entity

I'm having trouble configuring entity relationships when one entity inherits from another. I'm new to ADO Entity Framework -- perhaps someone more experienced has some tips for how this is best done. I'm using .net 4.
Database tables with fields:
Products (int ID, nvarchar Description)
FoodProducts (int ProductID, bit IsHuge)
Flavors (int ID, int FoodProductID, nvarchar Description)
There are constraints between Products and FoodProducts as well as FoodProducts and Flavors.
Using the designer I create a model from the database. The designer seems to get it right, with a 1:0..1 association between Product and FoodProduct entities, and 1:* association between Flavor and FoodProduct. No errors when I save or build.
Next I set FoodProduct entity to inherit from Product entity. Then I get errors concerning relationship between Product and FoodProduct. Ok, starting fresh, I first delete the relationship between Product and FoodProduct before setting the inheritance. But now I get errors about the relationship between FoodProduct and Flavor. So I delete and then recreate that relationship, connecting Flavor.ID to FoodProduct.ProductID. Now I get other errors.
My question is this: Should I instead be creating relationship between Flavor.FoodProductID and Product.ID? If so, I assume I then could (or should) delete the FoodProduct.ProductID property. Since my database will have many of these types of relationships, am I better off first creating the entity model and exporting the tables to SQL, or importing the database schema and then making many tweaks?
My intent is that there will be several types of products, some of which require many additional fields, some of which do not. So there may be zero or one FoodProducts records associated with each Product record. At least by my thinking, the table for each sub-type (FoodProducts) should be able to "borrow" the primary key from Products (as a FK) to uniquely identify each of its records.
You can find a screen capture here: http://img218.imageshack.us/img218/9720/entityframework.jpg (I'd embed the img but haven't earned the requisite rep' yet!)
Well, I deleted the FoodProduct.ProductID field, as it should always return the same value as Product.ID anyway. Then, as you hinted, I had to manually map the Products.ID field to FoodProducts.ProductID field. Errors resolved. I'll write a little code to test functionality. Thanks for the "observations"!
Couple of observations:
FoodProducts needs a primary key (e,g identity - FoodProductID). Are you sure it should be a 1:0..1 between Food and FoodProducts? I would have thought it should be 1:0..*. For this cardinality to work you need a unique PK on this table.
When you setup inheritance for entities, the parent entity's properties are inherited. So FoodProducts will inherit ID from the Product table.
BUT, on the physical model (database), this field still needs to be mapped to a column on the FoodProducts table - which is why you need the identity field.
After you setup inheritance, you still need to map all the columns on the derived tables. My money is on you have not mapped "ID" on FoodProducts to any column.
If you screencapped your model and show the errors you are getting it would be much easier to diagnose the issue.