Help with Spring not adding to the database the entity

@PostMapping(value = "/admin/brands")
public String addColor(@Valid @ModelAttribute(name = "brand") Brand brand, Errors errors, Model model, RedirectAttributes redirectAttributes) {
if (errors.hasErrors()) {
model.addAttribute(brand);
return "editors/brand";
}
System.out.println(brand.getName());
System.out.println(brand.getCharacters());
brandService.saveBrand(brand);
redirectAttributes.addFlashAttribute("successMessage", "Brand added successfully !");
return "redirect:/admin/brands";
}
@PostMapping(value = "/admin/brands")
public String addColor(@Valid @ModelAttribute(name = "brand") Brand brand, Errors errors, Model model, RedirectAttributes redirectAttributes) {
if (errors.hasErrors()) {
model.addAttribute(brand);
return "editors/brand";
}
System.out.println(brand.getName());
System.out.println(brand.getCharacters());
brandService.saveBrand(brand);
redirectAttributes.addFlashAttribute("successMessage", "Brand added successfully !");
return "redirect:/admin/brands";
}
so in here when i system outprint everything is fine, the issue is that it doesnt save it in the database ? im losing my mind i dont know what the fix is, the saveBrand simply is:
public void saveBrand(Brand brand) {
brandDB.save(brand);
}
public void saveBrand(Brand brand) {
brandDB.save(brand);
}
and brandDB is :
package web5.spring.hello.Repository;

import org.springframework.data.jpa.repository.JpaRepository;

import web5.spring.hello.Model.Brand;


public interface BrandDB extends JpaRepository<Brand, String> {

}
package web5.spring.hello.Repository;

import org.springframework.data.jpa.repository.JpaRepository;

import web5.spring.hello.Model.Brand;


public interface BrandDB extends JpaRepository<Brand, String> {

}
18 Replies
JavaBot
JavaBotā€¢3w ago
āŒ› This post has been reserved for your question.
Hey @userexit! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1stā€¢3w ago
Is there any exception?
userexit
userexitOPā€¢3w ago
no everythign goes smoothly just the brand doesnt get added šŸ¤·ā€ā™‚ļø im also using this converteR:
@Component
public class CharacterConverter implements Converter<String, Character> {
@Autowired
private CharacterService characterService;

@Override
@Nullable
public Character convert(String source) {
System.out.println("Converting converting hehe");
try {
System.out.println(characterService.getCharacter(source));
return characterService.getCharacter(source);
} catch (Exception e) {
return null;
}
}

}
@Component
public class CharacterConverter implements Converter<String, Character> {
@Autowired
private CharacterService characterService;

@Override
@Nullable
public Character convert(String source) {
System.out.println("Converting converting hehe");
try {
System.out.println(characterService.getCharacter(source));
return characterService.getCharacter(source);
} catch (Exception e) {
return null;
}
}

}
dan1st
dan1stā€¢3w ago
Can you enable SQL logging? Maybe it is in the DB already?
userexit
userexitOPā€¢3w ago
nah it isnt im checking the db with h2-console i have sql logging
dan1st
dan1stā€¢3w ago
Maybe show what happens? Otherwise I'm just guessing
userexit
userexitOPā€¢3w ago
well the brand gets added successfully it just doesnt add the characters to the db
dan1st
dan1stā€¢3w ago
oh so it's about the relation Can you show the brand object before inserting it? And please also show the relevant entities
userexit
userexitOPā€¢3w ago
The Brand entity:
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Brand {
@Id
@NotBlank(message = "Brand name can't be empty")
private String name;
@OneToMany(mappedBy = "brand", cascade = CascadeType.ALL)
@ToString.Exclude
@EqualsAndHashCode.Exclude
private List<Character> characters;
}
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Brand {
@Id
@NotBlank(message = "Brand name can't be empty")
private String name;
@OneToMany(mappedBy = "brand", cascade = CascadeType.ALL)
@ToString.Exclude
@EqualsAndHashCode.Exclude
private List<Character> characters;
}
The Character entity:
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Character {
@Id
private String name;
@ManyToOne(cascade = CascadeType.ALL)
private Brand brand;
@JsonBackReference
@ToString.Exclude
@ManyToMany(mappedBy = "characters")
@EqualsAndHashCode.Exclude
private List<Design> designs;
@OneToMany(mappedBy = "character", cascade = CascadeType.ALL)
private List<CharacterVersion> characters;

public void addCharacterVersion(CharacterVersion cversion) {
if (characters == null) {
characters = new ArrayList<>();
}
this.characters.add(cversion);
}
}
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Character {
@Id
private String name;
@ManyToOne(cascade = CascadeType.ALL)
private Brand brand;
@JsonBackReference
@ToString.Exclude
@ManyToMany(mappedBy = "characters")
@EqualsAndHashCode.Exclude
private List<Design> designs;
@OneToMany(mappedBy = "character", cascade = CascadeType.ALL)
private List<CharacterVersion> characters;

public void addCharacterVersion(CharacterVersion cversion) {
if (characters == null) {
characters = new ArrayList<>();
}
this.characters.add(cversion);
}
}
No description
userexit
userexitOPā€¢3w ago
No description
dan1st
dan1stā€¢3w ago
So Character has a private List<CharacterVersion> characters;?
userexit
userexitOPā€¢3w ago
yeah there are different versions of each character Goku can be SSJ1, SSJ2 etc
dan1st
dan1stā€¢3w ago
oh I wasn't reading it properly
userexit
userexitOPā€¢3w ago
but the issue was there before I implemented that
ayylmao123xdd
ayylmao123xddā€¢3w ago
sigma sigma on the wall whos the skibiddiest of them all do you use the transactional annotation on savebrand
userexit
userexitOPā€¢3w ago
no do I need to ?
ayylmao123xdd
ayylmao123xddā€¢3w ago
i mean you can try and see if it works
JavaBot
JavaBotā€¢3w ago
šŸ’¤ Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?