monasca-common/java/monasca-common-hibernate/src/main/java/monasca/common/hibernate/db/AbstractUUIDPersistable.java
Tomasz Trębski 6c56dc799e [monasca-common] Hibernate support added
- refactored code to match java guidelines
- added missed indexes
- fixed invalid string lengths according to latest mon.sql from ansible-monasca-schema
- introduced relations
- using ordinal for enum fields

Change-Id: Ic84bf542c10ff714885c7a53bb9cbe3a2fb728cd
2015-08-31 07:20:35 +00:00

98 lines
2.5 KiB
Java

/*
* Copyright 2015 FUJITSU LIMITED
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
*/
package monasca.common.hibernate.db;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import com.google.common.base.Objects;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import monasca.common.hibernate.core.Persistable;
import monasca.common.hibernate.type.BinaryId;
import monasca.common.hibernate.type.BinaryIdType;
@DynamicInsert
@DynamicUpdate
@MappedSuperclass
@TypeDef(
name = "monasca.common.hibernate.type.BinaryId",
typeClass = BinaryIdType.class
)
abstract class AbstractUUIDPersistable
implements Persistable<BinaryId> {
private static final long serialVersionUID = 6192850092568538880L;
@Id
@Type(type = "monasca.common.hibernate.type.BinaryId")
@Column(name = "id", length = 20, updatable = false, nullable = false)
protected BinaryId id;
AbstractUUIDPersistable() {
super();
this.id = null;
}
AbstractUUIDPersistable(final BinaryId id) {
this.id = id;
}
protected AbstractUUIDPersistable(final byte[] id) {
this(new BinaryId(id));
}
@Override
public BinaryId getId() {
return this.id;
}
@Override
public Persistable<BinaryId> setId(final BinaryId id) {
this.id = id;
return this;
}
@Override
public boolean isNew() {
return this.id == null || this.id.getBytes() == null;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AbstractUUIDPersistable that = (AbstractUUIDPersistable) o;
return Objects.equal(this.id, that.id);
}
@Override
public int hashCode() {
return Objects.hashCode(id);
}
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("id", id)
.toString();
}
}