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
Related
The Json POST request looks like this:
{
'title':'Star Wars: The Empire Strikes Back',
'description':'Darth Vader is adamant about turning Luke Skywalker to the dark side.',
'actors':[
{
'lastName':'Ford',
'name':'Harrison'
},
{
'lastName':'Hamill',
'name':'Mark'
}
]
}
So my Spring Boot Application just wants to store this whole json as a "Film" class and inside it has an inline array of "Actors". Here is the Film model:
#Entity
public class Film {
#Id
#GeneratedValue
private long id;
private String title;
private String description;
private ArrayList<Actor> actors = new ArrayList<>();
I have a separate entity for the Actor that looks similar:
#Entity
public class Actor {
#Id
#GeneratedValue
private long id;
private String name;
private String lastName;
Finally, I am using the RequestBody Annotation in the PostMapping in the Controller:
#PostMapping(value= "/api/film")
#ResponseStatus(HttpStatus.CREATED)
public Film addFilm(#RequestBody Film film) {
service.createFilm(film);
return film;
The problem is I always get the java.io.NotSerializableException that Actor cannot be serialized. I tried making Actor a Static inline class but that did not change anything. Anyone have an idea what is wrong here ?
Your Actor class needs to implement Serializable.
#Entity
public class Actor implements Serializable{
#Id
#GeneratedValue
private long id;
private String name;
private String lastName;
I use h2 in memory db and I don't want to create duplicate locations in my DataBase. Only when I use createItem and input location column id manualy it write it to the same location. Otherwise even if the country city gps coordinates are the same app write it to other location with it's id.
I tried to understand but It's not working
I got these entities.
#Entity
#Table(name = "item_System_items")
public class Item {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String title;
private String description;
private BigDecimal price;
private Integer stock;
#ManyToOne
#JoinColumn(name = "location_id")
#Cascade(value={org.hibernate.annotations.CascadeType.ALL})
private Location location;
And
#Entity
#Table(name = "item_System_locations")
public class Location {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String country;
private String city;
private String street;
private String gpsCoordinates;
SETTERS AND GETTERS IS THERE I JUST NOT POST THEM HERE
Controller
#RestController
#RequestMapping("/items")
public class ItemsController {
#Autowired
private ItemsService service;
#PostMapping
#ResponseStatus(value=HttpStatus.CREATED)
public int createItem(#RequestBody Item item) {
return service.createItem(item);
}
Service
#Service
#Transactional
public class ItemsService {
#Autowired
private ItemJPARepository repository;
public int createItem(Item item) {
return repository.save(item).getId();
}
I expect after re-coding app doesn't make new location if the column values are the same.
Thank you people!
If you really help me I would be so happy!
There is nothing in your Entity definitions to tell the ORM about the constraint you want.
You can add #UniqueConstraint to the #Table in your Location entity and specify which column(s) must all be in a unique combination (example given in the linked documentation):
#Table(
name="EMPLOYEE",
uniqueConstraints=
#UniqueConstraint(columnNames={"EMP_ID", "EMP_NAME"})
)
This will add a check in the database, if the ORM is managing your database schema, which will throw an exception when violated.
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.
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?
Hi I could need a little help understanding how I should approach following:
Just for information I'm working with play framework 2.0 and Ebeans version ~2.7
I have a model projects and a model reports. A project can have many reports but a report can only belong to one project.
This is my Projects Model:
#Entity
#Table(name = "Projects")
public class Projects extends Model {
private static final long serialVersionUID = -3343133304437498550L;
#Id
public long id;
#NotNull
#Size(min = 1, max = 255)
public String name;
#NotNull
#Size(min = 1, max = 255)
public String description;
#Formats.DateTime(pattern = "dd/MM/yyyy")
public Date created_at = new Date();
public static Finder<Long, Projects> find = new Finder<Long, Projects>(Long.class, Projects.class);
}
And this is my Reports Model:
#Entity
#Table(name = "Reports")
public class Reports extends Model {
private static final long serialVersionUID = -6617128169471038678L;
#Id
public long id;
#Constraints.Required
public String description;
#Formats.DateTime(pattern = "dd/MM/yyyy")
public Date created_at = new Date();
#ManyToOne
#NotNull
public Projects project;
public static Finder<Long, Reports> find = new Finder<Long, Reports>(Long.class, Reports.class);
}
The saving works without a problem I can create a Project and I can create multiple reports that point to the right projects. My question know is how would I go about querying this.
When I have a project how would I get all related reports to it? I think I could figure it out as a plain SQL-Query but I'm pretty sure it should be possible with ebean.
Best regards and thank you for your time!
UPDATE:
This is how I would do it with a sql query
SELECT * FROM `Projects` LEFT JOIN `Reports` ON Projects.`id` = Reports.`id`;
I can't try it right now, but I would do something like this:
public class Reports extends Model {
...
public static List<Reports> findByProject(Projects project) {
return find.fetch("project").where().eq("project.id", project.id).findList();
}
}