Skip to content

Commit

Permalink
fixup! hibernate 6 upgrade adaptation
Browse files Browse the repository at this point in the history
  • Loading branch information
mbussolotto committed Dec 12, 2024
1 parent 8ccedec commit 0caa25f
Show file tree
Hide file tree
Showing 12 changed files with 55,734 additions and 66 deletions.
14 changes: 7 additions & 7 deletions java/code/src/com/redhat/rhn/domain/channel/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ public class Channel extends BaseDomainHelper implements Comparable<Channel> {
private String supportPolicy;
private String updateTag;
private boolean installerUpdates;

private Set<ClonedChannel> clonedChannels;
private Set<SUSEProductChannel> suseProductChannels;
private ChannelSyncFlag channelSyncFlag;
Expand Down Expand Up @@ -621,7 +620,7 @@ public void removePackage(Package packageIn, User user) {
ChannelManager.removePackages(this, list, user);
}

/*
/**
* Some methods for hibernate to get and set channel families. However,
* there should be only one channel family per channel.
*/
Expand All @@ -642,11 +641,8 @@ public Set<ChannelFamily> getChannelFamilies() {
* @param channelFamiliesIn The set of channelFamilies
*/
public void setChannelFamilies(Set<ChannelFamily> channelFamiliesIn) {
if (channelFamiliesIn.size() > 1) {
throw new TooManyChannelFamiliesException(this.getId(),
"A channel can only have one channel family");
}
this.channelFamilies = channelFamiliesIn;
channelFamilies.clear();
this.channelFamilies.addAll(channelFamiliesIn);
}

/**
Expand Down Expand Up @@ -1026,6 +1022,10 @@ public void setInstallerUpdates(boolean installerUpdatesIn) {
installerUpdates = installerUpdatesIn;
}

/**
* @return the clonedChannels
*/

/**
* Returns all cloned channels of this channel which includes all clones of clones.
* @return all cloned channels
Expand Down
4 changes: 2 additions & 2 deletions java/code/src/com/redhat/rhn/domain/config/ConfigChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
* ConfigChannel - Class representation of the table rhnConfigChannel.
Expand Down Expand Up @@ -68,8 +69,7 @@ public class ConfigChannel extends BaseDomainHelper implements Identifiable {
@JoinColumn(name = "confchan_type_id", nullable = false)
private ConfigChannelType configChannelType;

@OneToMany(mappedBy = "configChannel", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@OrderBy("sort")
@Transient
private SortedSet<ConfigFile> configFiles = new TreeSet<>();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
import com.redhat.rhn.domain.user.legacy.UserImpl;
import com.redhat.rhn.manager.system.ServerGroupManager;

import org.hibernate.annotations.Cascade;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
Expand All @@ -37,7 +41,7 @@
@DiscriminatorValue("null")
public class ManagedServerGroup extends ServerGroup {

@ManyToMany
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinTable(
name = "rhnUserServerGroupPerms",
joinColumns = @JoinColumn(name = "server_group_id"),
Expand Down
34 changes: 18 additions & 16 deletions java/code/src/com/redhat/rhn/domain/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.cobbler.SystemRecord;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.Where;
import org.hibernate.annotations.WhereJoinTable;

import java.net.IDN;
import java.sql.Timestamp;
Expand Down Expand Up @@ -90,6 +91,7 @@
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderColumn;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;
Expand Down Expand Up @@ -260,7 +262,7 @@ public class Server extends BaseDomainHelper implements Identifiable {

@ManyToMany
@JoinTable(name = "suseServerAppstream",
joinColumns = @JoinColumn(name = "rhn_server_id"),
joinColumns = @JoinColumn(name = "id"),
inverseJoinColumns = @JoinColumn(name = "server_id"))
private Set<ServerAppStream> appStreams = new HashSet<>();

Expand All @@ -273,15 +275,15 @@ public class Server extends BaseDomainHelper implements Identifiable {
name = "rhnServerConfigChannel",
joinColumns = @JoinColumn(name = "server_id"),
inverseJoinColumns = @JoinColumn(name = "config_channel_id"))
@Where(clause = "position > 0") // JPA equivalent of the 'where' condition from XML
private List<ConfigChannel> configChannels = new ArrayList<>();
@WhereJoinTable(clause = "position > 0")
private List<ConfigChannel> configChannelsHibernate = new ArrayList<>();

@ManyToMany
@JoinTable(
name = "rhnServerConfigChannel",
joinColumns = @JoinColumn(name = "server_id"),
inverseJoinColumns = @JoinColumn(name = "config_channel_id"))
@Where(clause = "position IS NULL") // JPA equivalent of the 'where' condition from XML
@WhereJoinTable(clause = "position IS NULL")
private Set<ConfigChannel> localChannels = new HashSet<>();
@Transient
private VirtualInstance virtualInstance;
Expand Down Expand Up @@ -502,8 +504,8 @@ public void setSandboxOverride(ConfigChannel ch) {
*/
protected void setConfigChannelsHibernate(
List<ConfigChannel> configChannelsIn) {
configChannels = configChannelsIn;
configChannels.removeIf(Objects::isNull);
configChannelsHibernate = configChannelsIn;
configChannelsHibernate.removeIf(Objects::isNull);
}

/**
Expand All @@ -512,12 +514,12 @@ protected void setConfigChannelsHibernate(
* @return List of config channels
*/
protected List<ConfigChannel> getConfigChannelsHibernate() {
return configChannels;
return configChannelsHibernate;
}

protected List<ConfigChannel> getConfigChannels() {
ensureConfigManageable();
return configChannels;
return configChannelsHibernate;
}

/**
Expand All @@ -536,7 +538,7 @@ protected List<ConfigChannel> getConfigChannels() {
* @return A stream of the ServerConfigChannels mappings
*/
public Stream<ConfigChannel> getConfigChannelStream() {
return getConfigChannels().stream();
return getConfigChannelsHibernate().stream();
}

/**
Expand All @@ -555,15 +557,15 @@ public Stream<ConfigChannel> getConfigChannelStream() {
* @return A list of the ServerConfigChannels mappings
*/
public List<ConfigChannel> getConfigChannelList() {
return new ArrayList<>(getConfigChannels());
return new ArrayList<>(getConfigChannelsHibernate());
}

/**
* @return Returns the number of configuration channels associated with
* the server.
*/
public int getConfigChannelCount() {
return getConfigChannels().size();
return getConfigChannelsHibernate().size();
}

public void setHasConfigFeature(Boolean hasConfig) {
Expand Down Expand Up @@ -623,7 +625,7 @@ public final void unsubscribeConfigChannel(ConfigChannel configChannel, User use
* @param user The user doing the action
*/
public void unsubscribeConfigChannels(List<ConfigChannel> configChannelList, User user) {
configChannelList.forEach(cc -> configListProc.remove(getConfigChannels(), cc));
configChannelList.forEach(cc -> configListProc.remove(getConfigChannelsHibernate(), cc));
}

/**
Expand All @@ -632,7 +634,7 @@ public void unsubscribeConfigChannels(List<ConfigChannel> configChannelList, Use
* @param user The user doing the action
*/
public void setConfigChannels(List<ConfigChannel> configChannelList, User user) {
configListProc.replace(getConfigChannels(), configChannelList);
configListProc.replace(getConfigChannelsHibernate(), configChannelList);
}

/**
Expand All @@ -643,10 +645,10 @@ public void storeConfigChannels() {
.setParameter("sid", getId())
.executeUpdate();

if (!configChannels.isEmpty()) {
String values = IntStream.range(0, configChannels.size())
if (!configChannelsHibernate.isEmpty()) {
String values = IntStream.range(0, configChannelsHibernate.size())
.boxed()
.map(i -> String.format("(%s, %s, %s)", getId(), configChannels.get(i).getId(), i + 1))
.map(i -> String.format("(%s, %s, %s)", getId(), configChannelsHibernate.get(i).getId(), i + 1))
.collect(Collectors.joining(","));


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ ON CONFLICT (server_id, server_group_id) DO NOTHING
""";

// Create a native query
Query<Object> query = HibernateFactory.getSession().createNativeQuery(sql, Object.class);
Query<ServerGroup> query = HibernateFactory.getSession().createNativeQuery(sql, ServerGroup.class);

// Set parameters for the query
query.setParameter("sgid", sgid);
Expand Down
36 changes: 22 additions & 14 deletions java/code/src/com/redhat/rhn/domain/server/ServerGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.redhat.rhn.manager.configuration.SaltConfigSubscriptionService;
import com.redhat.rhn.manager.configuration.SaltConfigurable;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.util.HashSet;
Expand Down Expand Up @@ -52,7 +54,7 @@
*/
@Entity
@Table(name = "rhnServerGroup")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) // Use single-table inheritance
@DiscriminatorColumn(name = "group_type", discriminatorType = DiscriminatorType.STRING)
public class ServerGroup extends BaseDomainHelper implements SaltConfigurable {

Expand All @@ -70,10 +72,6 @@ public class ServerGroup extends BaseDomainHelper implements SaltConfigurable {
@Column(name = "description", length = 1024)
private String description;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "group_type", insertable = false, updatable = false)
private ServerGroupType groupType;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "org_id")
private Org org;
Expand Down Expand Up @@ -144,6 +142,10 @@ public void setOrg(Org orgIn) {
this.org = orgIn;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "group_type", insertable = false, updatable = false)
private ServerGroupType groupType;

/**
* @return Returns the groupType.
*/
Expand Down Expand Up @@ -248,23 +250,29 @@ public Optional<Pillar> getPillarByCategory(String category) {
*/
@Override
public int hashCode() {
return Objects.hash(id);
return new HashCodeBuilder().append(getId())
.append(getName())
.append(getDescription())
.append(getOrg())
.append(getGroupType())
.toHashCode();
}

/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object o) {
// Compare the IDs of ServerGroup to check equality
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
public boolean equals(Object other) {
if (!(other instanceof ServerGroup castOther)) {
return false;
}
ServerGroup that = (ServerGroup) o;
return Objects.equals(id, that.id);
return new EqualsBuilder().append(getId(), castOther.getId())
.append(getName(), castOther.getName())
.append(getDescription(), castOther.getDescription())
.append(getOrg(), castOther.getOrg())
.append(getGroupType(), castOther.getGroupType())
.isEquals();

}

/**
Expand Down
27 changes: 3 additions & 24 deletions java/code/src/com/redhat/rhn/domain/server/ServerGroupType.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ public Set<Feature> getFeatures() {
return features;
}

/**
* @param featuresIn The features to set.
*/
public void setFeatures(Set<Feature> featuresIn) {
this.features = featuresIn;
}
Expand All @@ -149,28 +152,4 @@ public boolean isBase() {
public Entitlement getAssociatedEntitlement() {
return EntitlementManager.getByName(this.getLabel());
}

@Override
public String toString() {
return "ServerGroupType{id=" + id + ", label='" + label + "', name='" + name + "'}";
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

ServerGroupType that = (ServerGroupType) o;

return id != null ? id.equals(that.id) : that.id == null;
}

@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
}
2 changes: 1 addition & 1 deletion java/code/src/com/redhat/rhn/testing/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public static <T> T reload(T obj) throws HibernateException {
*/
public static void saveAndFlush(Object obj) throws HibernateException {
Session session = HibernateFactory.getSession();
session.saveOrUpdate(obj);
session.merge(obj);
session.flush();
}

Expand Down
Loading

0 comments on commit 0caa25f

Please sign in to comment.