Ich versuche, einen Produktkatalog zu erstellen, das Produkt hat -> Attribute -> OptionenFrühling JPA Multi-Table-Beziehungen
So würden die Daten wie folgt aussehen:
product_one
attribute_one
option_one
option_two
attribute_two
option_one
option_two
Der Code ist auf GitHub https://github.com/ccsalway/prod_info_mngr
ich habe eine Klasse für jede Entität erstellt:
@Entity
class Product {
@Id
@Generatedvalue
private Long id;
private String name;
// getters and setters
}
@Entity
class Attribute {
@Id
@Generatedvalue
private Long id;
private String name;
@ManyToOne(cascade = CascadeType.REMOVE)
private Product product;
// getters and setters
}
@Entity
class Option {
@Id
@Generatedvalue
private Long id;
private String name;
@ManyToOne(cascade = CascadeType.REMOVE)
private Attribute attribute;
// getters and setters
}
Ich habe ein Repository für jede Entität erstellt:
@Repository
public interface ProductRepository extends PagingAndSortingRepository<Product, Long> {
}
@Repository
public interface AttributeRepository extends PagingAndSortingRepository<Attribute, Long> {
}
@Repository
public interface OptionRepository extends PagingAndSortingRepository<Option, Long> {
}
ich einen Dienst für jede Entität erstellt haben:
@Service
public class ProductService {
// Autowired Repositories
// methods
}
@Service
public class AttributeService {
// Autowired Repositories
// methods
}
@Service
public class OptionService {
// Autowired Repositories
// methods
}
Ich habe für jede Entität einen Controller erstellt haben:
@Controller
@RequestMapping("/product")
public class ProductController {
@Autowired
private ProductService productService;
//methods
}
@Controller
@RequestMapping("/product/{prod_id}/attribute")
public class AttributeController{
@Autowired
private AttributeService attributeService;
//methods
}
@Controller
@RequestMapping("/product/{prod_id}/attribute/{attr_id}")
public class OptionController {
@Autowired
private OptionService optionService;
//methods
}
Und (endlich) habe ich mehrere Ansichten für jeden Controller erstellt (ich werde sie hier nicht hinzufügen).
Was ich versuche, in der product_view.jsp
Ansicht zu tun ist, eine Liste der Attribute zeigen und ihre zugehörigen Optionen, etwa wie folgt:
<table id="attrTable">
<thead>
<tr>
<th>Name</th>
<th>Options</th>
</tr>
</thead>
<tbody>
<c:forEach items="${attributes}" var="attr">
<tr data-id="${attr.id}">
<td>${fn:htmlEscape(attr.name)}</td>
<td><c:forEach items="${attr.options}" var="opt" varStatus="loop">
${opt.name}<c:if test="${!loop.last}">,</c:if>
</c:forEach></td>
</tr>
</c:forEach>
</tbody>
</table>
So ist der Tisch so etwas wie dieses
product_one
attribute_one option_one, option_two
attribute_two option_one, option_two
aussehen würde
Der erste Schritt war eine @RequestMapping
in ProductController
zu erzeugen:
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String view(Model model, @PathVariable Long id) {
Product product = productService.getProduct(id);
List<Attribute> attributes = productService.getAttributes(product);
model.addAttribute("product", product);
model.addAttribute("attributes", attributes);
return "products/product_view";
}
aber ${attr.options}
in der Ansicht erkennt nicht die options
-Taste, also wie gehe ich über das Machen dieser Arbeit?
Ich versuchte, einen @OneToMany
Verein in dem Product
Entity hinzuzufügen, aber diese erstellt, eine Tabelle in der Datenbank mit product_id|attribute_id
denen Sie dann das Attribute speichern müssen und dann das Produkt mit dem neuen Attribute aktualisieren, was auch bedeutet, wenn Sie wählen ein Produkt, Sie ziehen ALLE Attribute und ALLE Optionen, die Paging auf denen verhindert.
@Entity
class Product {
@Id
@Generatedvalue
private Long id;
private String name;
@OneToMany
List<Attribute> attributes;
// getters and setters
}
Haben die Entitäten sogar Beziehungen oder einfach 'id' und 'name'? – Synch
Entschuldigung, ja, ich werde die hinzufügen, da ich sie vermisst habe. – Christian
In Ihrem Controller sehe ich 'model.addAttribute (" Attribute ", Attribute);' während in Ihrem JSP '$ {attr.options}' ist es ein Tippfehler? Andernfalls denke ich, dass Sie '$ {attributes.options}' –