#SequenceGenerator not generating sequence - oracle12c

Whatever I am gonna say is very strange and very true, Please don't give negative marking.
Hibernet-core : 5.2.14
Spring -boot 2.0
DB : Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit
This should create a sequence by the name INSUREDACCESSORYID_SEQ in DB but it fails to do so when I start my server.
private #Id #GeneratedValue(strategy=GenerationType.SEQUENCE,generator="insuredAccessoryId_seq")
#SequenceGenerator(name="insuredAccessoryId_seq",sequenceName="INSUREDACCESSORYID_SEQ",allocationSize=1) Long id;
Strange Thing: If I change ( ADD / REMOVE ) a single/multiple alphabets
in value of property sequenceName= it will create a sequence. Changing a value of property will create a sequence.
Example :
INSUREDACCESSORYID_SEQ -->> not work
JINSUREDACCESSORYID_SEQ -->> Will create a sequence
Any help will be appreciated.

Related

Generate Hibernate Envers primary key manually and jpa as well

My project is migrating from Grails to Java so I have exisiting Audit data in a single table pushed by Grails Audit plugin, now I am using Java envers for Auditing in java. I have below doubts:
-> If i want to push data from single table to different Audit tables(in Java) manually, how can i generate Primary key of revision table for history data manually which will not collide with primary key generated from Java annotation?
As for new entries I am generating primary key like below:
#Id
#GeneratedValue
#RevisionNumber
#Column(name = "ID") #NotNull
private Long revisionId;
-> Any other way to push data from single audit table to segregated Audit tables(In java) instead of doing it manually?
Please let me know about this.
Thanks.
Envers should be creating sequence named REVISION_LISTENER on Oracle.
Therefore you should be able to use Oracle's syntax SELECT REVISION_LISTENER.NEXTVAL in order to get the next sequence value and use that in the REV field both in your audit table and the revision entity info table too.
This only applies to DefaultAuditStrategy.
If you intend to use the ValidityAuditStrategy, there are other fields which you'll have to understand the internals of Envers in order to be able to populate the relevant fields. If you are using this strategy, let me know and I'll add more details that are specific to it, but its very complex and quite tedious.

Why and how hibernate_sequence is using for id generatior

I'm faced out an issue with ID generation for my entities generated by JHipster:
Dev environment on H2 database
Prod environment on Postgres
I have one entity "station" with two fields "id" and "name"
Creating a liquibase script which imports dictionary in "station" table like
INSERT INTO station (name) VALUES ('Adygeya') without ID definition
Trying to add station on dev environment - OK
Trying to add station on prod - Hibernate tries to add new station with duplicated ID
WHY?
My research was shown that only for postgres and oracle in initial scheme jhipster created a new sequince "hibernate_sequence" which is used for new entities creation.
So I'm fixed out this wrong behaivour by adding specific sequence name for my entity ID generation rule
#Id
#GeneratedValue(strategy = GenerationType.AUTO, generator = "station_gen")
#SequenceGenerator(name = "station_gen", sequenceName = "station_id_seq")
private Long id;
Now I have just 3 questions:
Why JHipster using one sequince for all tables for postgres and oracle?
Where is it configured?
What am I doing wrong?
Will post here my answer wich was found with help of #GaƫlMarziou and #Julien Dubois.
Thats because of history and default behaivour of Hibernate hilo algorithm with GenerationType.AUTO. MySQL doesn't support sequencies so JHipster needs to use this stupid algorithm
JHipster team has found right solution wich will fix my problem. The solution is to use GenerationType.SEQUENCE for all DB but use GenerationType.IDENTITY for MySql. All the details in this commit https://github.com/jhipster/generator-jhipster/commit/4516b4ff4d49a96a75fd963b0c7667f198bd9b79
This way I will configure my entities now too.

Postgresql: How #GeneratedValue works in postgresql?

I am very new to postgresql. In playframework with Ebean, I have used mysql and auto generated value there was actually auto incremented in that case. The sequence I got was 1,2,3,4...
But in Postgresql, the generated value doesn't seem to have such property. It seems that for every session it is starting with a number larger that the previous session's number and then auto incremented. The sequence in this case, 1,21,41,42,61,81,101,102,103...
My code segment:
#Entity
public class Post extends Model{
#Id
#GeneratedValue
#Required
public int id;
I am using:
Playframework 2.3.4
Ebean
Postgres 9.3
I really don't know the reason. Is there any particular reason of such values? Is there any catch about this? Do I need to switch to auto increment? And if so, how do I do it?
K, so you're using AUTO strategy, as it's the default. I don't know much about Ebean, but with Hibernate, a sequence called hibernate_sequence is created.
This sequence would be shared by other entities, so that might be part of the reason for the gaps. Your ORM might also be pre-allocating sequence values for performance reasons.
Either way, I always use IDENTITY strategy with Postgres, even though Postgres doesn't use IDENTITY. It does however create a sequence per table, which is generally what you want.

ADO.NET INSERT new record that has a foreign key in the table

I am fairly new to ADO.NET. I am ok with all the basic INSERT, etc. But now, I have a problem inserting a record into a table that contains a foreign key. I have done some research but am still stuck ... so here goes:
I want to INSERT a new record into a table called Professionals. It has a foreign key mapped to a different table. The FK is WAPublicUserID.
See Image:
When I create a data model, the WAPublicUserID isn't listed in the Properties of the Professional data model.
See Image:
Therefore, when I try to create an INSERT in my code, the WAPublicUserID field can't be found and I can't insert the record. The WAPublicUserID that I wish to use already exists in the WAPublicUser table that the FK is mapped to.
See Image:
How do I go about Inserting a new record in the Professionals table that contains a foreign key to an existing record in the WAPublicUser table? Thanks!
Someone has set "Include Foreign Key Properties in Model" to false.
Hence you have the navigation property of WAPublicUser but not the ForeignKey property.
This means you will have to Attach the relevant WAPublicUser object to the WAPublicUser property on the object you are trying to save.
I'd need a lot more code to know exactly what you are doing, but the basics of it are as follows:
If the WAPublicUser already exists:
Grab the existing entity from the database - OldEntity.
Update the OldEntity with the properties of the new one you are currently trying to save.
Save the (now updated) Old entity back to the database - because you have just read it, it should have the WAPublicUser reference already set.
If it doesn't:
Create a new WAPublicUser
Set the WAPublicuser property of the Professional object to the newly created WAPublicUser - that line goes where your code stops above.
myEnt.AddToProfessionals(pro);
myEnt.SaveChanges();
Got it. Here's how it works, in case anyone else reads this. #1 and #2 are focal points.
Mucho thanks to #BonyT for getting me on the right path ...
using (JONDOEntities myEnt = new JONDOEntities())
{
// #1) Need to create WAPublicUser object first
var wap = (from w in myEnt.WAPublicUsers
where w.WAPublicUserID == 981
select w).FirstOrDefault();
var proUser = (from p in myEnt.Professionals
where p.WAPublicUser.WAPublicUserID == wap.WAPublicUserID
select p).FirstOrDefault();
// If the record does not exist in the Professional table, insert new record.
if (proUser == null)
{
JONDOModel.Professional pro = new JONDOModel.Professional()
{
ProfessionalType = "unknown",
FirstName = "unknown",
LastName = "unknown",
PhoneNumber = "unknown",
WebsiteUrl = "unknown",
TaxID = "unknown",
BusinessInfo = "unknown",
ProfessionalLogo = "unknown",
IsApproved = true,
CATaxExempt = false,
WAPublicUser = wap // #2) Plug in the WAPublicUser object here
};
myEnt.AddToProfessionals(pro);
myEnt.SaveChanges();
}
OK, here's the real answer to my OP:
The asp.net website that I took over to manage was targeting .NET 3.5. Apparently, there are some issues with 3.5 and Entity Framework.
I converted the website to target .NET 4.0(*see below to see how). When I went to create the entity data model, voila , by default, it now included the Foreign Keys and therefore, I did not have any issues as described in OP.
If you run into this situation, you have to make sure that the web server is also upgraded to .NET 4.0. Because if you upgrade/convert the website files to target .NET 4.0, but your web server hasn't been upgraded, then although the website runs smooth on your dev machine (assuming that it has .NET 4.0 framework), it will crash on the live web server.
As an aside, .NET 4.0 framework will run apps that were built using previous versions of .NET (backward compatibility) ... however, an app that targets .NET 4.0 will not run in an environment with .NET 3.5 framework or prior.
CONVERT WEBSITE TO .NET 4.0:
Two ways to convert/upgrade website to .NET 4.0. 1) Usually, when you open a fresh copy and it's targeting 4.0, visual studio will ask if you want to convert/upgrade (Choose YES). 2) Within visual studio (commercial version), click WEBSITE tab, START OPTIONS, BUILD ... then you should see the options to change the "Target Framework" ...

Postgresql/openJPA (geronimo) Sequence Issue

I am having a weird problem with a sequence. Im using postgresql 9 with geronimo 2.2. I have created the sequence PLANTS_ID_SEQ inside the db environment and when I try to create a new entity I get an error in my logs (which comes from postegresql) that the relation PLANTS_ID_SEQ exists. It seems that it tries to create the sequence that is already created. This is the code from the entity bean:
#Id
#GeneratedValue(generator="PLANTS_SEQ",strategy=GenerationType.SEQUENCE) #SequenceGenerator(name="PLANTS_SEQ", sequenceName="PLANTS_ID_SEQ",allocationSize=1) #Column(name = "ID")
private Integer id;
Please notice that if I change the sequence name (eg sequenceName="MY_SEQ")then the code runs correctly but it creates in postgresql (and obviously uses) the MY_SEQ sequence. If anyone has a clue about this case please share.
Thanks George
If your table has a column of type SERIAL, then postgres will create the sequence for you and use it automatically on inserts.
The sequence it creates is named "tablename_id_seq"...
Probably you're trying to duplicate what postgres has already done, and create a duplicate sequence.
Solved:
Should add in persistence.xml the following property:
property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(Sequences=false)"
this way, the MappingTool of openjpa will not try to create the sequence again.