I use Eclipse Hibernate Tools to create domain classes starting from my database and need to add JPA annotations.
Is there a way to add annotations? Possibly with reveng.xml and Reverse Engineering? How should this be done?
Generated domain code:
public class Country implements java.io.Serializable {
private long id;
private String description;
private String identifier;
private String futureuse;
private Set accounts = new HashSet(0);
public Country() {
}
public Country(long id, String description, String identifier) {
this.id = id;
this.description = description;
this.identifier = identifier;
}
...
Needed code:
#Entity
#Table(name = "COUNTRY")
public class Country implements java.io.Serializable {
#Id
#Column(name="CNTR_ID")
private Long id;
#Column(name="CNTR_FUTUREUSE")
private String futureUse;
#Column(name="CNTR_IDENTIFIER")
private String identifier;
#Column(name="CNTR_DESCRIPTION")
private String description;
private Set accounts = new HashSet(0);
public Country() {
}
public Country(long id, String description, String identifier) {
this.id = id;
this.description = description;
this.identifier = identifier;
}
...
I personally don't use hibernate tools, because I'm pretty happy with Spring Roo. However, google search brought me to this.
As mostly there is a nice tutorial from mkyong.com. If you go to "Hibernate perspective" and click "Code generation configuration" in the "Export" tab there is a checkbox for "Generate EJB3 annotations".
http://www.mkyong.com/hibernate/how-to-generate-code-with-hibernate-tools/
This was further confirmed in previous answers.
Can Hibernate tool generate JPA POJO?
Related
I want to create simply page where the vaadin's grid will be displayed with data from database. Unfortunately I didnt find any solution in documentation or movies... So, I have my JPA class:
#Entity
#Table
public class Movie {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column
private String movieName;
#Column
private String description;
public Movie(){}
public Movie(String movieName, String description) {
this.movieName = movieName;
this.description = description;
}
With all getters and setters. And now I want to create GUI:
#Route("show-movies")
public class MovieGUI extends VerticalLayout{}
And I tried everything: Grid < Movie>,initializeGrid, but nothing works. I just want to in simple way add 3 columns(name,desc and action) and display data from my db and button to create action. Does anyone know how to solve this problem?
Something along these lines:
#Route("show-movies")
public class MovieGUI extends VerticalLayout{
MovieGUI(MovieRepository repo) {
Grid<Movie> movieGrid = new Grid<>();
movieGrid.setItems(repo.findAll());
movieGrid.addColumn(Movie::getName).setHeader("Name");
movieGrid.addColumn(Movie::getDescription).setHeader("Description");
movieGrid.addComponentColumn(movie -> new NativeButton("Action", click-> doSomething(movie)).setHeader("");
add(movieGrid);
}
}
Check out the demo sources here for more examples: https://vaadin.com/components/vaadin-grid/java-examples
I have an entity with the following fields:
private Date dateOfBirth;
private String cityOfBirth;
private Long birthStateCodeId;
private Long birthCountryCodeId;
private Boolean isUSCitizen;
private Long citizenshipCountryCodeId;
private String address1;
private String address2;
private String addressCity;
private Long addressStateCodeId;
private Long addressCountryCodeId;
private String postalCode;
As you can see from the above snippet, I have
2 properties (birthStateCodeId, addressStateCodeId) where I use a state code from a StateCodes table, and
3 properties (birthCountryCodeId, citizenshipCountryCodeId, and addressCountryCodeId) where I use a country code from a CountryCodes table.
Using JPA (with Hibernate as persistence provider), how do I map the above 5 properties (2 state codes and 3 country codes) to the two separate tables StateCodes and CountryCodes?
You could achieve it like this:
#Entity
public class PersonIdentification {
// primary key
#Id // and other annotations, see JPA Spec or tutorials
private long id;
// regular attributes
private Date dateOfBirth;
private String cityOfBirth;
private Boolean isUSCitizen;
private String address1;
private String address2;
private String addressCity;
private String postalCode;
#ManyToOne
private StateCode birthStateCode;
#ManyToOne
private StateCode addressStateCode;
#ManyToOne
private CountryCode birthCountryCode;
#ManyToOne
private CountryCode addressCountryCode;
#ManyToOne
private CountryCode citizenshipCountryCode;
// setter & getter methods as needed...
}
Next, define entity classes for both "Code" types as such:
#Entity
public class StateCode {
// primary key
#Id // and other annotations, see JPA Spec or tutorials
private long id;
private String code;
private String stateName;
// other attributes of interest
// setter & getter methods as needed...
}
#Entity
public class CountryCode {
// primary key
#Id // and other annotations, see JPA Spec or tutorials
private long id;
private String code;
private String countryName;
// other attributes of interest
// setter & getter methods as needed...
}
To reduce CnP code (as with the generic aspect of primary key handling (#Id) you can check this answer. It gives you detailed hints on how handle such cases more efficiently by introducing an AbstractEntity via the #MappedSuperClass annotation.
Hope it helps
Background:
I am trying to model a many-to-many relationship using the embedded document model.
The reason behind choosing mongodb is because the schema is dynamic as the platform deals with various products and services.
The application when logged in by an user bootstraps all the information of a user. But, all the queries such as update,search are majorly done on the products and services associated with users on the platform.
Problem Statement:
#Getter
#Setter
#ToString(exclude = { "id", "password" })
#Document(collection = "users")
public class User implements Serializable {
#Id
private ObjectId id;
#Indexed(unique = true)
private String username;
private String password;
private String email;
private String mobile;
private Sex sex;
private List<String> tags;
}
public class Product {
#Id
private String id;
private String name;
private double price;
private List<User> users;
}
public class Service{
#Id
private String id;
private String name;
private double price;
private List<User> users;
}
Initially i thought of making users as embedded documents within products and services. The reason behind choosing the embedded document design is to avoid multiple queries during the search.
But, I also realized that when a user logs in to the application i need to have all the user related information which needs to be bootstrapped. The design seems to be getting complicated.
I would like to understand if this scenario could be handled in a better way. Anyone who has already solved this situation?
The queries i would like to perform are
Find:
db.products.find({"name": /.*m.*/})
Update:
.update(Products:{$elemMatch:{Bag:"charollette"}}, {$set: {'Products.$.Name':"Charollette blue"}});
Insert:
db.products.insertOne( { item: "card", qty: 15 } );
I'm building an application with Google App Engine. I'm using Objectify for Datastore persistence. I have two POJO class.
#Entity
public class Book
{
#Id private Long id;
private String name;
private String isbn;
private String author;
}
and
#Entity
public class Person {
#Id private String email;
private String password;
private String name;
private List<Book> myBooks;
}
A person can contain many books but book only belongs to one person. Currently I'm saving data like this.
//data from Front-end
Person p = new Person(email, password, name);
PersonDAO dao = new PersonDAO();
dao.save(p);
//...
//data from Front-end
Book b = new Book(name, author, isbn);
BookDAO daoBook = new BookDAO();
daoBook.save(b);
//...
Person q = dao.load(email);
q.addBook(b);
dao.save(q);
ObjectifyService ofy() with methods are implemented in DAO classes.
It's ok my implementation? How can I optimize the relationship? Every time that I need create a book this is saved like Entity but I need the relationship with a person, therefore I'm saving book twice. I've seen implementantions with Key, Ref #Load tags but I don't know how are working them.
Besides, Person POJO has a Date field. will It be saved normally?
It has being quite a while since I wrote this code, but here it goes:
#Entity
public class Book {
#Id
private Long id;
#Parent
#ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
private Key<User> user;
private String author;
private String isbn;
private String name;
}
Take a look at Udacity Conference Central for full example. It is the final project of Developing Scalable Apps in Java
with Google App Engine, by Udacity.
Spring LdapRepository save() method throws exception when I'm trying to update an existing object in LDAP database.
org.apache.directory.api.ldap.model.exception.LdapEntryAlreadyExistsException: ERR_250_ENTRY_ALREADY_EXISTS
What method should I use to update existing ldap objects?
Person class:
#Entry(objectClasses = { "inetOrgPerson", "organizationalPerson", "person", "top" })
public class Person implements Serializable {
public Person() {
}
#Id
private Name dn;
#Attribute(name = "cn")
#DnAttribute(value = "cn")
#JsonProperty("cn")
private String fullName;
#Attribute(name = "uid")
private String uid;
private String mail;
#Attribute(name = "sn")
private String surname;
//setters and getters
}
Person repo interface:
public interface PersonRepo extends LdapRepository<Person> {
}
That's how I'm updating person:
personRepo.save(person);
Default implementation for Spring LDAP repositories is SimpleLdapRepository, that checks the property annotated with #Id to determine if the objects is new - and perform create, or old - and perform update.
I'm guessing that Person.dn is null when you're trying to perform update.
You also can take the control over this by implementing org.springframework.data.domain.Persistable and place your logic in the isNew() method.
See the implementation details.