JPA table capitalization inconsistent - jpa

We're using a very basic JPA implementation that should create tables consistently from our models.
I believe we're using EclipseLink or TopLink (whichever one is default with the latest Netbeans/Glassfish). The problem is, the tables are created with inconsistent capitaliztion and with the columns out of order. For me, It creates the "User" table as "user", and for other members of my team it creates "USER".
I've tried using the #Table annotation (#Table(name="USer")), but it doesn't work.
How do we get EclipseLink to generate consistent table names? Frankly this seems like a rather amateurish mistake for a framework like this.
Sub-question : the reason this is a problem is because EclipseLink by default has no default way of managing schema/data migrations, as far as I know of. The way we're handling it is by writing a bunch of INSERT INTO's to bootstrap the objects we need in our database, and drop-and-recreating the tables every time the schema changes. I know this is not the best practice for propagating schema changes -- does anyone know how this is typically handled in a standard JPA implementation?
Thanks.

By default EclipseLink uses all upper case for the table name, the class User would be USER.
If you specify an #Table annotation with name="USer", then the table will be created as "USer".
Perhaps you are using your own scripts to create the tables, or you database is changing the case based on the OS or its own settings. What database are you using?
If you enable logging in EclipseLink, it will show the exact DDL that it is executing (if it is executing DDL).
In EclipseLink 2.4 there is also a "create-or-extend-tables" DDL generation option to alter existing tables.

We never found any good answer for this. Luckily, we found a workaround for the ways we were using to update the table, which didn't care about capitalization.

Related

Configure default schema for Doctrine 2.5 and PostgreSQL 10

I'm building a PHP application and I'm using Doctrine 2.5 to map my objects into the database. My database choice is PostgreSQL 10.
Instead of using the default schema (public), I've tagged all my classes with #ORM\Table(schema="my-schema") so it uses this schema instead of the default one. This works as intended: all the tables are created on that schema.
But I've found that tables for ManyToMany relations are not being created in my schema; instead they're created on the default one
(I suppose this is occurring because they're not backed up with a class on my model).
Then my question is if it's possible to configure somewhere the default schema I want Doctrine to use (this would also allow me to get rid of all the #ORM\Table(schema="my-schema") annotations). Maybe that is a bug on Doctrine, I don't know. Any kind of workaround to prevent this also would be appreciated.

How can I create an EF Code First data model for AdventureWorks2014?

The fact that EF does not handle the hierarchyid MSSQL data type is widely known. The problem using the AdventureWorks sample database to generate an EF data model caused by a table using hierarchyid is also quite well known and widely reported. A good proposed workaround, for read only scenarios, is to use a view in place of the 'offending' Production.Document table, and cast that column to a more acceptable type, like 'nvarchar'.
However, it seems the only avenue open for creating a Code First data model is to brutally remove the table altogether, as the EF Power Tools code generator skips the pleasantries allowing a user to select which tables to include in the model. So here, even using a view won't work, because the view depends on its base table.
Does anyone know a workaround for this that doesn't involve modifying the T4 templates by hard-coding to skip this table?
Yes, install VS 2013 Update 4, which include the latest EF Tools, and use the Code First Model from Database feature, it allows you to select which tables to generate.

My programs use Entity Framework. How can I add a field to a database table without having them throw exceptions?

My programs use Entity Framework. I want to add new fields to a modelled database table being referenced by these programs, but these programs won't use these new fields (others will use them). It appears that the existing programs start throwing exceptions, even though they don't reference these new fields. Is there a way around this?
My experience in the past has been that Entity Framework can ignore database fields as long as they are nullable. If the new fields are non-nullable, Entity Framework throws exceptions because it can't generate valid insert statements for them.
I think the answer (or perhaps work-around) is as follows (my experience is only using SQL Server, and using the SSMS to edit the database tables):
If you have a running system and you wish to add new fields to a database table, be sure to add those fields at the "end" of the table's definition, otherwise you will have to rebuild all of the running processes against the new table definition.
Just musing, I doubt this is the best solution.
You generate your model against views rather than tables, that will afford you the freedom to modify the underlying tables as required.

Change Schema of Entity Framework

I'm using Entity Framework 5 on ASP MVC 4 web site I'm developing.
Because I am using shared hosting which charge for the number of databases I use I would like to run a test site near my production site.
I have two problems:
1) I use Code First and Database Migration. The migration classes seem to embed the schema dbo inside the name of the tables.
How can I change the schema according to the test/production flag
2) How can I change the schema from which EF select data?
Thank you,
Ido.
Both migration and EF take schema from mapping so if you want to change the schema you must update your mapping to use:
modelBuilder.Entity<MyEntity>().ToTable("MyTable", "MySchema");
and control the value of MySchema from configuration but this is really bad idea. One day you forget to change the value and break your production. Use local database for development and test.
As already said: use identical databases (structurally) for development, test and production.
The goal of schemas is to group database objects, like we do with namespaces in e.g. C#, or to simplify permissions for groups of database objects. Not to identify database stages. By using them for the latter you also make it much harder, if not impossible, to use schema appropriately. See for instance this MSDN white paper.
It is much easier to use some database name conventions to indicate their purpose.

jpa eclipselink modify field type

I'm making my first steps with JPA (eclipselink) developement, and i'm wondering if there's any good practice that may help reflecting changes brought to an entity into the data-base without re-creating the whole data-base.
In my case i have more than 30 entities, some of these are "correctly" defined and are already holding data rows on the data-base, but i've figured out that i made a mistake while defining some other entities by using the int type for the primary key instead of long, and the worst is that some of these entities are mapped by other entities, which means that they're used as foreign keys ... So i'm wondering if there's any "proper" way to deal with this without having to re-create the whole data-base. Re-creating the data-base will force me to re-fill the rows of the already filled tables and waist much time.
Thanks in Advance
George
ints and longs are mapped to a number column in database. Changing the type in the entities won't change anything. You might have to change the precision of the columns in database so that the number is sufficiently large to accept any long value, but doing that is trivial in SQL, using an alter statement.
JPA is just a way to access the database. The database is not supposed to be created by the JPA engine. You may do that, but you're not forced to do that. You may do what you want with your database using SQL, and do whatever you want with your Java code. If they both map together when running the application, it will work.
You can update your current schema automatically to reflect the modifications you've done inthe module by configuring EclipseLink like this:
<property name="eclipselink.ddl-generation" value="create-tables" />
For major modifications to your model it does not guarantee proper update to the schema, but for smaller ones it should work ok.