0

Traditionally, JPA ‘Entity’ classes are specified in a persistence.xml file. With Spring Boot this file is not necessary and instead ‘Entity Scanning’ is used

@Entity public class Hotel implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue private Long id; @ManyToOne(optional = false) @NaturalId private City city; @Column(nullable = false) @NaturalId private String name; @Column(nullable = false) private String address; @Column(nullable = false) private String zip; @OneToMany(fetch = FetchType.LAZY, mappedBy = "hotel") private Set<Review> reviews; protected Hotel() { } public Hotel(City city, String name) { this.city = city; this.name = name; } public City getCity() { return this.city; } public String getName() { return this.name; } public String getAddress() { return this.address; } public String getZip() { return this.zip; } } 

how create spring boot entity by tool? i don't want to write entity manual .

2
  • You can free up quite a bit of code using @Getter and @Setter (and more features) from project Lombok projectlombok.org/features/GetterSetter.html Commented Sep 25, 2015 at 4:06
  • There is no such thing as a Spring Boot Entity it is just a plain JPA entity like any other... Also traditionally you don't have to specify the entities in persistence.xml you could also have JPA scan them (which is also what spring uses when configuring an EntityManager). So everything Spring (Boot) does is already there by default (but making it easier). Commented Sep 25, 2015 at 10:18

2 Answers 2

3

First of all we are not talking about Spring Entities. But we create Java Persistence API Models; Annotated POJO. Further reading here: http://www.oracle.com/technetwork/articles/java/jpa-137156.html

If you want to create Entities from Database or Database ERM you can use the Eclipse Spring Tool Suite (STS) to do the Job for you.

The Spring STS includes a in the Eclipse JPA Perspective the "ENTITY FROM TABLE Wizard". That is a great tool wen you have a lof of Entities to be generated from DB.

  1. Goto Eclipse STS -> Window -> Perspective -> Open Perspective -> JPA (if you dont find directely search for that)

  2. Create a Database Connection if you dont have already one; to do that open Eclipse -> Window -> Show View -> Data Source Explorer Then go there and add a new Connection.

  3. Now you can select on your Project (or create a new JPA Project) and go with the File -> New -> JPA From Table (or orm if you have one :) )

After all you'll see everything you need to create the complete POJO Annotated Classes. That tool will save all your day :)

When the Creation has done, please have a look at the classes in the generated package of the selected Project and modify anything to meet you needs. Sometimes the Generator do a lot of more stuff we don't need so be aware about that.

Sign up to request clarification or add additional context in comments.

Comments

0

For do this, you can implement a your custom notation, i don't think is the best practice, use annotaion like @notNull for building a JSONObject if you need this solution

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.