I am using Java7 persistence with Spring Data JPA template to generate my DAL classes using Telosys generator. While my database has default values set for all the columns, my template fails to generate the same in my #column annotation.
Kindly suggest if this can be fixed in the template or any other template you suggest.
In Telosys 3.x "columnDefinition = xxx DEFAULT xx" is not handled by "$jpa" object.
It is supported in Telosys 4.0.
So you have 2 options :
you can switch to Telosys 4
you can managed the column definition yourself in the template (for example with a Velocity macro or with your own specific Java class)
Related
For generating setters and getters for multiple fields in a class, we have lombok. Similarly, do we have any framework or library to get columns from a table in db and generate it on an entity class? It would be really useful while working with tables with a lot of columns instead of writing each field individually.
Not a library but a function in the IDE.
In Eclipse right-click on a project containing a persistence.xml
and select JPA-Tools -> Generate Entities from Tables...
How can i auto increment a non primary column using annotations.
I tried this but it is not working.
#Generated(value="GenerationTime.INSERT")
#GenericGenerator(name="id", strategy="sequence")
private Long id;
I am using mysql database
JPA specification only provides for "id" fields to have their values generated. Any other fields are not supported. Some implementations (e.g DataNucleus JPA) support value generation on any field, but that is a vendor extension.
You could use prePersist callback and hook something in yourself if your provider doesn't support it
PS those annotations you use are NOT JPA annotations
I'm using the grails db-reverse-engineer plugin to generate domain classes from an existing PostgreSQL database.
Each table in the database has an associated sequence that determines the next id. For example, the table "table" with the primary key column "t_id" uses the sequence "table_t_id_seq".
When I use db-reverse-engineer to generate domain classes, it generates a mapping as follows:
id column: "t_id", generator: "assigned"
This caused an error and after some searching and tinkering, I discovered that the appropriate mapping is as follows:
id column: "t_id", generator: "sequence", params: [sequence: "table_t_id_seq"]
Is there any way to configure db-reverse-engineer to generate the latter rather than the former? I'd rather not have to modify all the domain classes I generate to make them work.
No, that bit is hard-coded. You can create a feature request at http://jira.grails.org/browse/GPREVERSEENGINEER
I know that you can change T4 templates used to generate the classes using Entity Framework 5.0 if you are using Model First. How would you change the template used to generate the tables when you use code first?
When using Code First there are no templates to generate tables. To set table names you can use TableAttribute or configure the table for the given entity with ToTable method in your OnModelCreating override.
The answer to my question should be quite obvious, but I cannot find it. I have a edmx file that has one table. There is a field of type string. EF always generates nvarchar for that (which is kind of expected), but I need an ntext instead of nvarchar for that field as 4000 is too small for me.
So tell me - what is the proper way to tell EF to generate ntext fields?
PS Using Entity Framework 4, SQL CE 3.5
I guess you are using model first, don't you? You can simply create custom T4 template for SQL DDL generation and include logic which will use NTEXT when field is defined with max size.
Default template is on:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\DBGen\SSDLToSQL10.tt
Just copy this template and find the logic where data type is created. Once you have your template change DDL Generation Template in model properties (in the designer) to your modified version.
There is much more you can do with generation template because you can add some annotations to your model (XML) and use them for custom logic in the SQL generation process.
Just set the property "MaxLength" in the Designer to "Max". This will generate a ntext field in the SQL CE DB.
If your project contains an ADO.Net Entity Data Model (.edmx) then see Ladislav's excellent answer.
But if you're using the Code First libraries and your project doesn't contain a .edmx then you can use the System.ComponentModel.DataAnnotations.ColumnAttribute to specify the column type:
using System.ComponentModel.DataAnnotations;
public class Note {
[Column("Note", TypeName="ntext")]
public string Note { get; set; }
}