Spring Roo Web MVC Insert - No Default Primary Key - spring

I have a web MVC project generated by Spring roo. I reverse engineered a MSSQL DB and have the need to create my own primary keys but am unable to insert using the inputs Roo generated (anticipates an auto increment or self-generated ID). Any ideas? I recieve the below message from the console when I attempt this.
ERROR org.hibernate.util.JDBCExceptionReporter - Field 'id' doesn't have a default value

It appears that the DBRE code generation for #ManyToOne/#JoinColumn is a bit incorrect. The code generator incorrectly sets such a field with "insertable = false" which then omits this particular foreign key column reference during an insert into the referencing table. Change it to "insertable = true" as well as the "updateable" if you require and you should be good.

Related

entity framework without primary key

I have created an entity class for my mvc application. But, my table doesn't have a primary key and the VS throws back an error:
"The table/view does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view."
Is there a way to still use the model even if it has no primary key??
or Do I have to add a pk? or should I just use execute command?
Any suggestion/comment is highly appreciated. Thanks in advance. :)

Adding columns to DB after publishing using EF CodeF and mvc nugget Scaffold

I have created a web site using mvc 3 and Ef code first , now after publishing the site and it's DB I have found out that I need to add a new columns to one of my DB table,
(the DB already has a lot of data in it )
should I add the columns direct to the DB or should I add to the class?
(just a simple string with get and set)
And how can I do it without losing my data in the DB ?
thanks
Adding the columns to the class should be enough. Evidence you can find here.
Here is the full list of changes that migrations can take care of automatically:
Adding a property or class
Nullable columns will be assigned a value of null for any existing rows of data
Non-Nullable columns will be assigned the CLR default for the given data type for any existing rows of data
Renaming a property or class
See ‘Renaming Properties & Classes’ for the additional steps required here
Renaming an underlying column/table without renaming the property/class
(Using data annotations or the fluent API)
Migrations can automatically detect these renames without additional input
Removing a property
See ‘Automatic Migrations with Data Loss’ section for more information
I suggest you to add the columns direct to the DB and to the class, and test it on the local machine.

Spring Roo Oracle and Underscore

I have a question concerning spring roo and databases.
I have a field called personName, in oracle to column is create as person_Name
I there a way to avoid the underscore. I suppose naming my column personname would fix this, but can I ask spring not to add the underscore ?
If you need a general solution (instead of "fixing" some single points (abaloghs answer)), you can specify a Naming Strategy for your JPA provider.
For an example see: JPA (Hibernate) and custom table prefixes
Roo by default refers to the JPA implementation to determine column names. You can override the defaults with the --column property:
entity --class Foo
field string --fieldName FooBar --column fooBar
Bonjour,
by the way, I do not think that it is possible to reverse engineer a database with underscores in table names :
the corresponding domain classes will be created and compiled since Java accept undersocres in class names
the tests will be performed without raising any issue
everything will be scaffold for the GUI
you will succesfully deploy it on tomcat and your application page will show up in your browser
You may fill the form to create a new instance of your object
But if you click on SAVE --> internal error
If you have a look at the tomcat log, you will fid the well known exception : javax.servlet.jsp.JspTagException: No message found under code ...
The reason is that your class name has been truncated in the message_xx.properties files.
Everything before the underscore is dropped and thus, no message is found in order to display that your record has been successfully saved.
It would be nice that the ROO shell raise an error when the jpa entity is created and not at runtime ...

entity framework returning only one value but the list size is correct

Entity framework returning only one value but the list size is correct
I have a table that does not have primary id and I need to get or select all the values in it.
What I see is when I do the selection with linq the number of objects is correct but it is the first row over and over.
I am simply doing something like this
List<MyValueType> valuesInDB = myDb.MyValueTypes.ToList();
Problem is I may get thousands of rows (which is correct) but the rows all have the same exact data.
I am using VS 2010 and used the wizard to create my EF object.
The problem is that entity framework is not able to work with entity without a key. So if your table doesn't specify a key, entity framework will infer its own. The key created by EF is composed of all non-nullable non-binary columns.
So if you for example have single non-nullable column in your entity which have only very small set of values (like enum) you will be able to load only single entity "per value". The reason is an inner implementation of the context and the state manager which uses Identity map pattern. When data record is retrieved from database, EF will first check an entity key and tries to find an object with the same key in its internal storage. If an object is found it will use that object instead of data record retrieved (despite of different data). If an object with the key is not found a new object is materialized and added to internal storage.
That is the purpose of Identity map - object with given key should be created only once by each context. Identity map is core pattern in ORM.
I wrote about Identity map also in this question.
I would suggest searching for the word "Warning" in your EDM's designer.cs file. It might tell you if Entity Framework is having any issues with your table.
I really can't comment much in the absence of the table design. I tried replicating your problem but wasn't able to do so. Here is what I did:
Created a table with no primary key but it had a unique key on an ID column. Entity Framework was able to infer a primary key and when I fetched the data, I not only got the correct number of rows but also the corrects data in those rows.
Created a table with no primary key and no unique key. Also there was no column called ID. Entity Framework excluded this table in the EDM that was generated. Consequently I wasn't able to query this table at all.This was displayed as a warning in the EDM designer file.
It would be better if you can share the create script for your table.

Castle ActiveRecord Seeding Primary Key Value

I am wondering how to 'seed' an auto incrementing primary key value using Castle AR? For Example wanting the Orders table primary keys to start out as 10000. Is this something that is 1. possible 2. a good solution for creating order numbers?
Maybe there is a way to have consecutive auto incrementing field on the DB that is NOT the pk, seeded to 10000?
Castle ActiveRecord is built on top of NHibernate and features of AR heavily rely on features of NHibernate. NHibernate contains several primary key generators:
1. native - This is the default generator. If you specify this then NHibernate automatically chooses generator type based on underlying database. For example, if I would have used native instead of identity in the above mapping snippet you will still get the same SQL because NHibernate is smart enough to understand that the underlying database SQL Server and it supports identity columns. NHibernate converts the returned values using Convert.ChangeType method.
2. identity - This can be used with Identity columns provided with SQL Server, MySQL, Sybase etc.,
3. sequence - Firebird, DB2, PostgreSQL, Oracle, SAP DB supports sequences
4. increment - This generator does not uses any database feature like sequence or identity. NHibernate automatically increments 1 to last primary key value. This generator is helpful when dealing with single database system but it does not help in cluster based environment.
5. hilo - Hi/Lo algorithm is used to generate primary key values. This is very efficient when compared to other generator types. When used, NHibernate creates a separate table named hibernate_unique_key and creates a column named next_hi and then NHibernate uses this table as a reference when INSERT happens. We will talk elaborately on this later in this post.
6. uuid.hex - Uses System.Guid and its ToString method for generating string based primary key values.
7. guid - This can be used when the class property type is Guid.
8. guid.comb - This is similar as guid but uses a different algorithm to produce primary key values. Note that uuid.hex, guid, guid.comb uses UNIQUEIDENTIFIER as a column data type in SQL Server.
9. assigned - last but not least, this generator assumes that the primary key value is assigned by the user.
So you can see that there are no such build-in functionality. In order to create an order number you can use 2 ways:
1. select max order and manually set it
2. add some insert trigger to database
In my opinion you should use first way because in this way you will not rely on database. And you can reuse this functionality when you will need to move an object up or down. I'm usually using this way.

Resources