0

I have generated/mapped tables from database using IntelliJ.

Now I want to update the database for one table and I would like to do it via ORM (JPA).

I have created entity class:

@Entity
@Table(name = "protocol", schema = "swprojekt")
public class ProtocolEntity {

    private int protocolId;
    private String name;
    private Date expires;
    private String description;
    private DocumentEntity document;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "protocol_id", nullable = false)
    public int getProtocolId() {
        return protocolId;
    }

    public void setProtocolId(int protocolId) {
        this.protocolId = protocolId;
    }

    @Basic
    @Column(name = "name", nullable = false, length = 3000)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Basic
    @Column(name = "expires", nullable = false)
    public Date getExpires() {
        return expires;
    }

    public void setExpires(Date expires) {
        this.expires = expires;
    }

    @OneToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="document")
    public DocumentEntity getDocument() {
        return document;
    }

    public void setDocument(DocumentEntity document) {
        this.document = document;
    }

    @Basic
    @Column(name = "description", nullable = false, length = 3000)
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof ProtocolEntity)) return false;

        ProtocolEntity that = (ProtocolEntity) o;

        if (getProtocolId() != that.getProtocolId()) return false;
        if (!getName().equals(that.getName())) return false;
        if (!getExpires().equals(that.getExpires())) return false;
        if (!getDescription().equals(that.getDescription())) return false;
        return getDocument().equals(that.getDocument());
    }

    @Override
    public int hashCode() {
        int result = getProtocolId();
        result = 31 * result + getName().hashCode();
        result = 31 * result + getExpires().hashCode();
        result = 31 * result + getDescription().hashCode();
        result = 31 * result + getDocument().hashCode();
        return result;
    }

However, now I am unsure. How can I force ORM to create the table? Running just app does not work. What configuration do I have to make/set?

Clijsters
  • 4,031
  • 1
  • 27
  • 37
Darlyn
  • 4,715
  • 12
  • 40
  • 90
  • 1
    well it depends on the JPA implementation you are using. For hibernate you can use the update ddl command or create command but be very careful because you can risk it will always create the DB. Give a look here https://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do – Angelo Immediata Jan 29 '18 at 15:28
  • Be very careful about using JPA to update an existing database. It can get confused and improperly modify your schema in non-straightforward scenarios. Consider using a tool like Flyway instead. – Taylor Jan 29 '18 at 15:32
  • Why do you need to use the ORM for table creation ? It's a very bad idea ! Using an orm to create tables is commonly considered bad practice for some optimization and maintenance reasons. – Valentin Michalak Jan 29 '18 at 15:34
  • You didn't mention which JPA implementation you are using. Please read about [mcve]. – Clijsters Jan 29 '18 at 15:36

0 Answers0