i have my sql server database and i used "generate persistence mapping" in intellij to get my java classes, but the relationships defined on my sql server database were not mapped, it seems i can add them with the "add relationship" but i have a lot of tables with a lot of relationships so it is a pain in the ass to do it...
is there something that can map the database including the relationships? for example if i have a table user with a defined relationship of type one to many with a table potatoes, get a java class like this :
public class user{
private int iduser;
private String name;
private String password;
//relationship!!
private ArrayList<Potatoes>potatoes; //this is mapped like "private int id_potatoes" by intillij
public user(){}
//getters and setters
}
i´m using open JPA. amd i also can't find a way to auto generate DAO's with some basic methods (save, find, delete), i will have to code them myself!!! and im talking of atleast 40 tables!! it is ridiculous!!
i know there MUST be a way to do all this stuff automatically. oh, im using jsf 2, i dont know if that is relevant to this question anyway.
when you generate persistence mapping, just check the below box option
匚 Show default relationships
by this to generate , you will get the table relationships you want. Hope to help you :)
Related
Could someone provide the list of similar annotations as of hibernate to be used for database models with reactive client? for example, join, id etc. etc.
public class Country {
int id; // Autogenerated incremental unique ID
String name;
List<City> cities; //List of cities where country has many cities under it
}
There is no solution at the moment for a Hibernate for reactive. But work on a Hibernate Reactive solution offering the JPA annotations is ongoing. https://github.com/hibernate/hibernate-rx/
I want to connect to a database with lot of tables with Java Spring MVC.
Currently, I am coding one class for each table. Ex;
#Table(name = "sa_user") //table name
public class SaUser implements Serializable {
private static long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "sa_user_id")
private Integer saUserId;
...
...
}
Do I have to write these class for all of the tables. This is a huge task. What is the best way?
There are some utilities which ease the task of creating the Entities whiche represent the database tables.
I usually use one called MinuteProject, which is a small java desktop app which opens a kind of assistant to connect to the database and generates all the Entities representing the database Tables, even including relationships, foreign keys, etc... between them.
I have been using it for more than acouple of years already and I feel comfortable with it. This is a screenshot of it's main view:
There is a way of doing it from eclipse too, though I have never used it:
https://www.eclipse.org/webtools/dali/docs/3.2/user_guide/tasks006.htm
I'm using LINQ to SQL to access my database but I'm only reading, I never insert, update or delete anything. Are there ways to optimize LINQ2SQL for this?
Yes there is. Linq 2 SQL will by default cache all data that you read from the DB. It needs to do this to track any changes you apply to your objects, so it can generate the necessary insert/update/delete statements when you call SubmitChanges()
If you're only reading data, this is unnessecary. You can turn off object tracking by setting the ObjectTrackingEnabled property to false on your DataContext.
One thing I've been told, is to avoid using the generated record class.
That is, if you have a Users table, L2S will create for you a User class, which is what it returns from the database. Instead of using that directly, you should create a "shadow" class --- all the same Properties, but nothing else, and immedaitely copy the data into those records for your use. In fact, if it's going to be exclusively read-only, you can assign them in the ctor, and only have public getters:
class myUser
{
public string FName {get; private set}
public string LName {get; private set}
public myUser(User user)
{
this.FName = user.FName;
this.LName = user.LName;
}
}
var users = from u in db.Users
where .....
select new myUsers(u);
This avoids a lot of overhead needed to deal with the possibility of writing the object out again.
I have a standard update happening via linq to sql but the data does not persist to the database.
I am using an auto-generated class via the .dbml file designer.
The update statement is below:
public static void UpdateEmailsInWorkingTable(Guid emailGuid, string modifiedEmail)
{
using (EmailDBDataContext DBContext = new EmailDBDataContext())
{
EmailAddress_Update EAUpdated = (from e in DBContext.EmailAddress_Updates
where e.EmailGuid == emailGuid
select e).SingleOrDefault();
EAUpdated.EmailAddress = modifiedEmail;
EAUpdated.IsValid = 'Y';
EAUpdated.UpdateFlag = true;
EAUpdated.LastChangedDtTm = DateTime.Now;
try
{
DBContext.SubmitChanges(ConflictMode.FailOnFirstConflict);
}
catch (ChangeConflictException ex)
{
// do stuff here
}
}
}
I looked through my auto-generated DataContext class and the only glaring difference is that the table in question EmailAddress_Update does not implement the two interfaces INotifyPropertyChanging and INotifyPropertyChanged that the other auto-generated entities do.
I am assuming that this is the cause of why the changes are not being persisted is it not???
To put it simply none of the Extensibility Method Definitions get generated for any part of this one class. If this is the cause of my problems, what in the database would be causing this to not auto-generate properly??
Thanks~
I posted this question on MSDN as well here: MSDN Linq to Sql if you wanted to see the replies. But I found part of the reason why the code doesn't generate.
Here is a piece from my MSDN response:
I created a small test table without a primary key and added it to the designer and sure enough it didn't generate any of the Extensibility methods for that instance.
So I then added a primary key to the same table and re-added it to the designer and sure enough all of the extensibility methods and change tracking events were generated.
My question now is why must there be a primary key for this stuff to auto-generate?
Ok so to answer my own question "My question now is why must there be a primary key for this stuff to auto-generate?" I found it in the book Pro LINQ written by Joe Joseph C. Rattz, Jr.
I was reading how to handle views versus tables and he says this:
"Because the entity classes generated for views do not contain entity class properties that are mapped as primary keys, they are read-only. If you consider that without primary keys, the DataContext has no effective way to provide identity tracking, this makes sense."
Mystery and problem solved.
So it appears with a simple designer change I can make the 3 tables implement the same interface without any additional coding, from here is it possible to get the rows to behave as queryable objects of the interface type without pulling the whole table down into memory and row by row cast?
each table can be treated as such:
[Table(Name="dbo.bLog")]
public partial class bLog : IBlogRow
where the interface is:
public interface IBlogRow
{
String App{get;set;}
String Version{get;set;}
String Message{get;set;}
DateTime dt{get;set;}
int? value {get;set;}
String ex {get;set;}
int? SecsTaken{get;set;}
bool? loanLevel{get;set;}
}
I think this is a contravariance or covariance problem. I can't seem to cast the table as a system.linq.table nor cast it as IQueryable ? How would I get this working so that i can write the same query code for all 3 tables that have the same design, and use them interchangeably?
Have you tried something along these lines?
IEnumerable<IBlogRow> GetRows()
{
DB db = new DB();
return db.BlogRows.Where(row => row.App == "something").Cast<IBlogRow>();
}
I think you'll have to cast after performing your query since LINQ to SQL won't be able to map IBlogRow properties to SQL table columns.
Update:
You could also try Dynamic Linq. You should be able to reuse most of the query code between the 3 tables. The drawback here is that you would give up strongly typed queries for magic strings.
Another option is using a PredicateBuilder. Take a look at the Generic Predicates example. This looks a lot like what you're describing.