1

I want to create a table in MySQL database that have a boolean column with values 'active' and 'inactive'. How can i do that?

My Entity class:

@Entity
@Table(name = "organization")
public class OrganizationEntity {
    
    private Long id;
    private String nameEntity;
    private String provinceEntity;
    private String supporterEntity;
    private String supporterAddressEntity;
    private boolean active;

    @Id
    @GeneratedValue
    @Column(name = "id")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "name")
    public String getNameEntity() {
        return nameEntity;
    }

    public void setNameEntity(String nameEntity) {
        this.nameEntity = nameEntity;
    }

    @Column(name = "province")
    public String getProvinceEntity() {
        return provinceEntity;
    }

    public void setProvinceEntity(String provinceEntity) {
        this.provinceEntity = provinceEntity;
    }

    @Column(name = "supporter_name")
    public String getSupporterEntity() {
        return supporterEntity;
    }

    public void setSupporterEntity(String supporterEntity) {
        this.supporterEntity = supporterEntity;
    }

    @Column(name = "supporter_address")
    public String getSupporterAddressEntity() {
        return supporterAddressEntity;
    }

    public void setSupporterAddressEntity(String supporterAddressEntity) {
        this.supporterAddressEntity = supporterAddressEntity;
    }

    @Column(name = "active")
    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }
}

My organization entity class have a boolean 'active' field that shows an organization is active or inactive. Now how can I have a column in database table for that?

3
  • 1
    Do you by any chance miss the tags JPA and JAVA ?? Commented Jan 21, 2015 at 8:28
  • The following google query... google.ch/search?q=jpa+boolean+mysql What have you tried yet? Commented Jan 21, 2015 at 8:29
  • Have you tried adding a BIT(1) or TinyInt(1) column? Commented Jan 21, 2015 at 8:31

2 Answers 2

1

Technically MySQL does not have a boolean type. BOOL and BOOLEAN converts to TINYINT(1).

From the MySQL documentation:

A value of zero is considered false. Nonzero values are considered true

You should be able to use the TINYINT(1) column from your code as some languages handle 1 as true and 0 as false (unless overwritten by you).

Not sure what language you are using (C# ?) you can try the following:

@Column(name = "active")
public boolean isActive() {
    return Convert.ToBoolean(active);
}

This is untested, so give it a go.

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

Comments

0

You can simply use boolean primitive type (but be sure you have NOT NULL column) or Boolean wrapper for nullable column. JPA providers (Hibernate or EclipseLink) are smart enough to convert that behind the scenes.

Use for field access type:

@Basic(optional = false)
@Column(name = "active")    
private boolean active;

public boolean isActive() {
    return active;
}

public void setActive(boolean active) {
    this.active = active;
}

or even for property access type:

private boolean active;

@Basic(optional = false)
@Column(name = "active")        
public boolean isActive() {
    return active;
}

public void setActive(boolean active) {
    this.active = active;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.