JPA How to manually generate an identifier for an entity? - spring-data-jpa

How to manually generate an identifier for an entity?
Is it possible to do it the way of select ID max+1 ? in service
ex) Entity id = A00001, A00002, A00003

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.

Peristence using Open JPA ;Setting composite primary key to Id Attribute of Main entity

Server - IBM WAS 8.5
Open JPA 2.2.3
I am trying to perform a container managed persistence using entity manager.
I have the following three entities defined
Entity MainEntity
Has an id of the type MainEntityPK
MainEntityPK - has the SubEntity1Code from SubEntity1 table and SubEntity2Code from SubEntity2 Table.
Entity SubEntity1 - SubEntity1Code - primary Key
Entity SubEntity2 - SubEntity2Code - primary Key
I try to create an entity of MainEntity which has an existing SubEntity1 and SubEntity2 entries
I first find the entities of SubEntity1 and Seg using
SubEntity1 SubEntity1 =
Entitymanager.find(SubEntity1.class,SubEntity1Code)
SubEntity2 subEntity2 =
Entitymanager.find(SubEntity2.class,SubEntity2Code)
The fetch are successful and I have both the instances of subEntity1 and subEntity2
Now I try to set the Primary Key MainEntityPK as follows
MainEntityPK MainEntityPK = new
MainEntityPK(SubEntity1Code,SubEntity2Code);
MainEntity MainEntity = new MainEntity();
MainEntity.setId(MainEntityPK);
I am getting following error :
Caused by: <openjpa-2.2.3-SNAPSHOT-r422266:1802534 nonfatal user error> org.apache.openjpa.persistence.ArgumentException
: Field: “model.MainEntity.id” of “model.MainEntity#e8df5623” can not be set to “model.MainEntityPK#1768a2” value.
I tried removing the setId call and was getting error as follows:
Caused by: <openjpa-2.2.3-SNAPSHOT-r422266:1802534 nonfatal user error> org.apache.openjpa.persistence.ArgumentException
: Field: “model.MainEntity.id” od “model.MainEntity#e8df5623” can not be set to null value.
Can some one help me in identifying what is wrong in setting the instance of MainEntityPK to id attribute of MainEntity

Specifying One-to-one relationship foreign key in JPA

I am implementing REST API using Spring and JPA. Consider for example the following scenario where there are Project and Department entities where a Project belongs to a single Department. I would normally have a Department object referenced in Project Pojo, with a #OneToOne annotation.
When creating a Project through REST API (where a Department is already created), I am currently getting departmentID as an attribute from the user, loading Department object using the ID, associating it to the Project and then saving the Project instance using JPA. Is there a way to avoid this and directly save the Project by specifying department ID directly?
Create two entity classes like
class Department{
#OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL,
mappedBy = "department")
Project project;
}
class Project{
}

Entity framework Association in conceptual model for one to many relationship

I have two entity CUSTOMER and ORDER..there is one to many relation from CUSTOMER to ORDER where CustomerID is primary key for customer and foreign key in ORDER..now I want to add customer name property from CUSTOMER entity in ORDER entity...I have copied this property and paste it in ORDER table and have added CUSTOMER table and map this property to the CUSTOMER table's same property..but when i trying to validate it vs giving me a Error that is
3024: Problem in mapping fragments starting at line 239:Must specify
mapping for all key properties (ORDER.OrderID) of the EntitySet ORDER
That is not possible in mapping. You cannot add property from Customer table into Order entity this way. Mapping properties from multiple tables to the same entity has very strict rules and it is not possible for this case.
You can expose customer's name in your Order class without defining it in the mapping. Create partial part of Order class and add custom computed property (non mapped):
public partial class Order
{
public string CustomerName
{
get
{
// Customer is navigation property to Customer entity
return Customer.Name;
}
}
}
This will require loading Customer with your Order (eager loading) or using lazy loading. Also this property cannot be used in Linq-to-entities queries.

How to add unique non primary-key field to entity using Entity Framework 4.1?

We are using Entity Framework 4.1 Code first.
We've got user entity with primary key set to UserId and need UserLogin to be unique. How can it be done?
Entity Framework does not support Unique constraints. You can create them using a SQL Query to generate unique constrains when initializing the database. Write your custom initializer for the model and execute SQL command to generate constrain.
Edit
Now (EF 6.1 onwards )you can easily have unique constrains ,
[Index("UserLoginIndex", IsUnique = True)]
public string UserLogin { get; set; }
Check out the unique constraints, I think that's what you're looking for?
http://weblogs.asp.net/manavi/archive/2011/01/23/associations-in-ef-code-first-ctp5-part-3-one-to-one-foreign-key-associations.aspx